Ape as an Android program when you see the JSON data format that is given back to you:
{ "id": 123, "url": "Http://img.donever.com/aa/bb.jpg", "width": $, "height": Likecount ": 1, " description ":" Hehe ", " Time ": 1234567890, " Replycount ": 0, " Floorcount ": 0, " Likeusercount ": 5, " age ": +, " name ":" Jack ", " school ":" Beijing ", " type ": 1, " sax ":" Boy ", "userid": 1223}
is not to silently create a class, and then a variable of private, and then get () +set ()?
If a JSON data provides attributes 20+ bar or a bar, a property to write, but also to ensure that the letter is not written wrong, the case is right, is not both a waste of time and energy, then try to use the Gsonformat plugin bar
Now learn how to use this plugin:
1, Android Studio open a project, click on the upper left corner File-->settings ... To set
2, choose Plug-in plugins, search Gsonformat, if you have not downloaded this plugin, then the search box will show "Nothing to show." Click Browse to ....
3, then click on the blue font of the Browse bar, this time will appear as the interface, we just need to select Gsonformat on the left and then click on the Right green button "Install plugin" on it.
4, completed the above three steps, you can use the Gsonformat plug-in
How to use it,
(1) Create a class file, the class name is to see what you need to customize write
(2) shortcut key Alt+insert, the following selection box appears
(3) We click on the first option, Gsonformat, a new box will appear,
Then just need to copy the server to your JSON data format, as shown below, click OK (Note that the JSON format does not make mistakes, such as not missing the comma after each property)
(4) The last step, there is a box, where you can make the corresponding edits, such as the type of a property change, or remove the blue bottom of the property before the white tick, so that the class does not have a property
Effect class:
public class People {/** * id:123 * url:http://img.donever.com/aa/bb.jpg * width:500 * Height: * likecount:1 * Description: Hehe * time:1234567890 * replycount:0 * floorcount:0 * lik Eusercount:5 * age:14 * name:jack * school:beijing * type:1 * sax:boy * userid:1223 */private int id; Private String URL; private int width; private int height; private int likecount; Private String description; private int time; private int replycount; private int floorcount; private int likeusercount; private int age; private String name; Private String School; private int type; Private String sax; private int userid; public static People Objectfromdata (String str) {Gson Gson = new Gson (); return new Com.google.gson.Gson (). Fromjson (str, people.class); } public void setId (int id) {this.id = ID; } public void SetUrl (STRing URL) {this.url = URL; } public void SetWidth (int width) {this.width = width; } public void setheight (int height) {this.height = height; } public void Setlikecount (int likecount) {this.likecount = Likecount; } public void SetDescription (String description) {this.description = description; } public void settime (int time) {this.time = time; } public void Setreplycount (int replycount) {this.replycount = Replycount; } public void Setfloorcount (int floorcount) {this.floorcount = Floorcount; } public void Setlikeusercount (int likeusercount) {this.likeusercount = Likeusercount; public void Setage (int.) {this.age = age; } public void SetName (String name) {this.name = name; } public void Setschool (String school) {This.school = school; } public void SetType (int type) {this.type = type; } public void Setsax (StRing sax) {this.sax = sax; } public void Setuserid (int userid) {this.userid = userid; } public int getId () {return id; } public String GetUrl () {return URL; } public int getwidth () {return width; } public int getheight () {return height; } public int Getlikecount () {return likecount; } public String GetDescription () {return description; } public int GetTime () {return time; } public int Getreplycount () {return replycount; } public int Getfloorcount () {return floorcount; } public int Getlikeusercount () {return likeusercount; } public int Getage () {return age; } public String GetName () {return name; } public String Getschool () {return school; } public int GetType () {return type; } public String Getsax () {return sax; } public int GetUserid () {return UserID }}people
If you want to use Gson parsing, you get the corresponding class object directly from the JSON string.
public static People Objectfromdata (String str) { Gson Gson = new Gson (); return Gson.fromjson (str, people.class); }
Remember to add in the Build.gradle
Compile ' com.google.code.gson:gson:2.4 '
Android Studio Excellent plugin: Gsonformat