how to parse json array using Google gson -
i've following json array , wanted parse 3 email address 3 separate variables. i wanted done using google gson library. how can ?
string jsonarrays=
[ { "id": "0e9b73bc-cf32-48b4-88cc-ee4ff8c3a823", "firstname": "chris", "lastname": "rogers", "height": "180cm", "weight": "74kg", "location": "uk", "email": "chris.rogers@test.com" }, { "id": "0e9b73bc-7cd7-48b4-88cc-ee4ff8c76vcv", "firstname": "lavina", "lastname": "langer", "height": "170cm", "weight": "63kg", "location": "poland", "email": "lavina.rogers@test.com" }, { "id": "213jdsj-cf32-48b4-1119hshjakakaaka", "firstname": "mike", "lastname": "harford", "height": "184cm", "weight": "80kg", "location": "paris", "email": "mike.harford@test.com" } ]
i've write code
gson mygson = new gson(); jsonparser jsonparser = new jsonparser(); jsonarray userarray =jsonparser.parse(jsonarrays).getasjsonarray(); list messagearray = new arraylist(); (jsonelement auser : userarray ){ user atwitteruser = mygson.fromjson(auser, user.class); messagearray.add(atwitteruser); }
the error getting
com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_object string @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:200) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$1.read(reflectivetypeadapterfactory.java:103) @ com.google.gson.internal.bind.reflectivetypeadapterfactory$adapter.read(reflectivetypeadapterfactory.java:196) @ com.google.gson.gson.fromjson(gson.java:810) @ com.google.gson.gson.fromjson(gson.java:875) @ com.google.gson.gson.fromjson(gson.java:848) @ com.altruista.mp.rest.messages.messagestest.testapimessages(messagestest.java:113) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:47) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:44) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26) @ org.junit.runners.parentrunner.runleaf(parentrunner.java:271) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:70) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:50) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238)
obviously json wrong com.google.gson.jsonsyntaxexception states. try removing last comma every object. maybe gson doesn't that. use http://jsonlint.com validate json.
Comments
Post a Comment