When parsing JSON data using Gson, if a property is null (and there is no error message), first check that the property name of the JSON file is consistent with the attribute name of the entity class.
As an example (normally, read a local JSON file here):
JSON file
{"Name": "Tim","age": "Max","job": "Basketball","hobby": "Dai"}
The parsing code is as follows
String jsonstring=fileutil.readassets (CTX, "Tim.json"); Gson g=new Gson (); Tim=g.fromjson (jsonstring, Tim.class);
Entity classes are as follows
Package Com.example.jsondemoerror;public class Tim {private string Name;private string Age;private string Job;private Str ing hobby;public String getName () {return name;} public void SetName (String name) {this.name = name;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;} Public String Getjob () {return job;} public void Setjob (String job) {this.job = job;} Public String Gethobby () {return hobby;} public void Sethobby (String hobby) {this.hobby = hobby;} @Overridepublic String toString () {return "Tim [name=" + name + ", age=" + Age + ", job=" + job+ ", hobby=" + hobby + "]"; }}
The parse succeeds when the name of each property of the JSON file is the same as that of the entity class.
If there is a inconsistent name:
1, the name of the JSON file is wrong, the entity class is not wrong, such as hobby written as Hobb, appeared Tim [Name=tim, age=40, Job=basketball, Hobby=null].
2, the entity class attribute name is wrong, JSON is not wrong, for example, write hobby as Hobb, appear Tim [Name=tim, age=40, Job=basketball, Hobb=null].
Second, using Gson can directly parse out the collection of entity classes.
JSON file
{"Name": "Tim","age": "Max","job": "Basketball","hobby":[{"name": "Sing", "Time" : "1"},{"name": "Dance", "Time": "1"},{"name": "Swim", "Time": "1"},{"name": "Dai", "Time": " 1 "}]}
Entity classes are as follows
PackageCom.example.jsondemoerror;Importjava.util.List; Public classTims {PrivateString name; PrivateString age; PrivateString Job; PrivateListHobby; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getage () {returnAge ; } Public voidsetage (String age) { This. Age =Age ; } PublicString getjob () {returnjob; } Public voidsetjob (String job) { This. Job =job; } PublicListGethobby () {returnHobby; } Public voidSethobby (listHobby) { This. Hobby =Hobby; } @Override PublicString toString () {return"Tims [name=" + name + ", age=" + Age + ", job=" +Job+ ", hobby=" + hobby + "]"; } }
Parsing code
String jsonstring=fileutil.readassets (CTX, "Tims.json"); Gson g=new Gson (); Tims=g.fromjson (jsonstring, Tims. Class); LOG.D ("jsonerroractivity", Tims.tostring () +tims.gethobby (). Get (0). GetName ());
Log content
Tims [Name=tim, age=40, Job=basketball, Hobby=[hobby [Name=sing, time=1], hobby [Name=dance, time=1], hobby [Name=swim, TI Me=1], Hobby [Name=dai, time=1]]]sing
Third, using Gson can parse out the entity class.
JSON file
{"Name": "Tim","age": "Max","job": "Basketball","hobby":{"name": "Sing", "Time": "1"}}
View Code
Entity class
PackageCom.example.jsondemoerror; Public classTimsh {PrivateString name; PrivateString age; PrivateString Job; PrivateHobby Hobby; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getage () {returnAge ; } Public voidsetage (String age) { This. Age =Age ; } PublicString getjob () {returnjob; } Public voidsetjob (String job) { This. Job =job; } PublicHobby Gethobby () {returnHobby; } Public voidsethobby (Hobby Hobby) { This. Hobby =Hobby; } @Override PublicString toString () {return"Timsh [name=" + name + ", age=" + Age + ", job=" +Job+ ", hobby=" + hobby + "]"; } }
View Code
Parsing code
String jsonstring=fileutil.readassets (CTX, "Timsh.json"); Gson g=new Gson (); Timsh=g.fromjson (jsonstring, Timsh. Class); LOG.D ("jsonerroractivity", Timsh.tostring () +timsh.gethobby (). GetName ());
View Code
Log content
Timsh [Name=tim, age=40, Job=basketball, Hobby=hobby [Name=sing, time=1]]sing
A summary of JSON parsing problems