In Android development, JSON is a client and server-side interaction of a language, it is simple syntax, the best is the current market has a very convenient wheel to analyze him. For example, Gson is a library provided by Google to parse or generate JSON, and it is convenient to map JSON strings directly to the corresponding entity classes. Let me summarize the usage of parsing JSON using Gson and the problems I have encountered.
Parsing of the simplest objects:
For example, this JSON string below:
{ text: "Love", img:"http://img2.imgtn.bdimg.com/it/u=2346639372,3797158284&fm=21&gp=0.jpg"}
If I want to parse it, I can define an object directly, such as:
class LoveJson{ String text;//这个名字要与json字符串中的相对应才能解析出来 String img;}
Then call Gson when parsing is needed to parse:
String jsonString;//待解析的json字符串new Gson();LoveJson love = gson.from(jsonString,Love.class);
Just a few lines will be resolved, the wheel is so convenient. Then we look at a few more complex analysis, and finally, I would like to talk about the problems I encountered and solutions.
Other more complex parsing: parsing of nested types
嵌套的Json类型无非就是这样的:
{ school:"12级一班" student_number:54 { student_name:"zhangsan" id:21 }}
Then the parsed class should be written like this:
class SchoolJson{ String school; String studentNumber; publicstaticclass Student{ String studentName; int id; }}
If you have obsessive-compulsive disorder, the variable has to use the Hump name method, then use annotations:
class SchoolJson{ String school; @SerializedName("studen_number") String studentNumber; publicstaticclass Student{ @SerializedName("student_name") String studentName; int id; }}
Parsing of array types
is also very simple, is a routine, such as:
[ { student_name:"zhangsan" id:1 }, { student_name:"lisi" id:2 }, ...]
class Student{ @SerializedName("student_name") String studentName; int id;}
This can be done at the time of parsing:
new Gson();Student[] students = gson.from(jsonString,Student[].class);
Principles for parsing JSON
May later meet more complex, such as objects inside nested arrays, arrays inside nested objects, objects inside and nested arrays of groups ... So there's a principle here:
When you encounter an object that is curly braces, write an object, and when you encounter an array, you can define a list<> and then the list<> to define a class that represents the elements of the array (if the array element is an object). So the loop goes on.
Let me give you a slightly more complicated example:
JSON string:
{stories: [{images: ["Http://pic1.zhimg.com/84dadf360399e0de406c133153fc4ab8_t.jpg"], type:0Id:4239728, Title:"the former Soviet Union Prison Tattoo Encyclopedia Handbook"}, {"Type":0,"id":7086807,"title":"Professional Staff Introduction Institute" · Unlocking the Autistic child "}, {"Images": ["Http://pic2.zhimg.com/afecdc04983a8e261326386995150599_t.jpg"],"Type":0,"id":7066097,"title":"The life cycle of the family: about" leaving Home "},...], Description:"The best thing to do for you is to find the most interesting things to see on WiFi.", Background:"Http://pic1.zhimg.com/a5128188ed788005ad50840a42079c41.jpg", Color:8307764, Name:"Don't be bored.", Image:"Http://pic3.zhimg.com/da1fcaf6a02d1223d130d5b106e828b9.jpg", editors: [{URL:"Http://www.zhihu.com/people/wezeit", Bio:"micro-editor in Wezeit"Id: -, Avatar:"Http://pic4.zhimg.com/068311926_m.jpg", Name:"Yi Kang glutinous rice"},...], Image_source:""}
The classes used for parsing can be written like this:
Public class Subjectdailycontentjson{ PublicList<story> stories =NewArraylist<> (); Public Static class story implements Serializable{List<string> images;intTypeintId String title; } PublicString description; PublicString background; Public intColor PublicString name; PublicString image; Publiclist<editor> editors; Public Static class Editor{String URL; String bio;intId String Avator; String name; }@SerializedName("Image_source") PublicString ImageSource;}
Is it easy to follow that principle? But recently I encountered a problem that makes me very unhappy, his array of elements actually not the same style, look at the top of the JSON string, his stories array of elements inside the format is not uniform, there is this:
{ images: [ "http://pic1.zhimg.com/84dadf360399e0de406c133153fc4ab8_t.jpg" ], type: 0, id: 4239728, title: "前苏联监狱纹身百科图鉴"},
There is this:
{ "type0, "id7086807, "title"职人介绍所 · 自闭儿童的解锁人" }
What to do, my egg hurts for a while, and finally found a way to solve the difference between the two of them is there is no images that array, then I simply have all written in my class inside, and then will those there is probably no attribute, I gave him a new value in advance, this way, If not, this property is null when JSON resolves to a class, so we just have to judge it. You see, my images that list I gave him a new one.
Today's summary of the use of Gson here, and later need to use the new knowledge of the words to supplement.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Using Gson to parse json in Android