Parse JSON data obtained from WebService with native methods from Android

Source: Internet
Author: User
Tags string to json


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.