The study of Oschina, the information obtained is XML, the feeling is not json so good, parsing too complex loop what, and XML is too much than JSON, trouble.
Tried to use Geon and Fastjson before, but always error. or using native JSON methods. I'm here to accept a set of JSON data
For example, we want to get a bunch of people information,
[ {"username": "Horse Halo", "Company": "Albaba"}, {"username": "Liu Qiang West", "Company": "Jingdong"}, {"username": "Horse Flower Hurts" , "Company": "QQ"}, {"username": "Li Yanhong", "Company": "Baidu"}]
These are the information for a group of people with two field names and company
Here we map based on the data we get: Generating a person class entity
public class User implements Serializable {private string Username;private string Company;public string GetUserName () {RET URN username;} public void Setusername (String username) {this.username = username;} Public String Getcompany () {return company;} public void Setcompany (String company) {this.company = Company;}}
How do we convert the JSON group data above into a group of people here, we can use list<user> to get the JSON data
So we're doing a userlist entity class used to transform JSON data for object groups
public class UserList implements Serializable {private list<user> userlist= new arraylist<user> ();// The total number of people in the information Group private int userscount;public int getuserscount () {return userscount;} Public list<user> getuserlist () {return userlist;} Parse JSON and convert to object group public static UserList parse (Jsonarray obj) throws jsonexception {userlist list = new UserList (); if (null! = OBJ) {//Get object group length as total number of List.userscount = Obj.length (); for (int i=0;i<obj.length (); i++) {Jsonobject Userjson = Obj.getjsonobject (i); User user = new user (), User.setusername (userjson.getstring ("username")), User.setcompany (Userjson.getstring (" Company ")); List.userList.add (user);}} return list;}}
The parsing parameter above is a group of JSON objects, so it's easy to get here.
String usersstring = "[ {" username ":" Ma Halo "," Company ":" Albaba "}, {" username ":" Liu Qiang West "," Company ":" Jingdong "}, {"username": "Horse Flower Hurts", "Company": "QQ"}, {"username": "Li Yanhong", "Company": "Baidu"}] ";
If the JSON data we get is a string, which is usually a string, then we convert it to Jsonarray using Java's own method.
/** * String to JSON * * @param json * @return * @throws jsonexception */public static Jsonarray Tojsonarray (string json) th Rows Jsonexception {return new Jsonarray (JSON);}
We also wrote a method for converting a string into a jsonarray.
Write here will not write is not very simple hope to give you a point.
Parse JSON data obtained from WebService with native methods from Android