Android parses json data, android parses json data

Source: Internet
Author: User

[Switch] parse json data for Android and parse json data for android

1. json format

2. json parsing 3. gson parsing 4. fastjson parsing 1. Json format a lightweight data exchange format. Xml and json are generally used for data transmission and exchange over the network. Two structures: 1) object (key-value set) {"id": "1001", "name": "zhangshan", "address": "shanghai"} 2) array (array object), with the key-value object included in []

{"Students": [{"sid": 1001, "name": "zhangsan", "addr": "Yichang", "pic": "111.jpg "}, {"sid": 1002, "name": "Jack", "addr": "Beijing", "pic": "222.jpg"}]}

 

Or:

{[{"Sid": 1001, "name": "zhangsan", "addr": "Yichang", "pic": "111.jpg"}, {" sid ": 1002, "name": "Jack", "addr": "Beijing", "pic": "222.jpg"}]}

2. json data generation and parsing of JSONObject represents a JSON object that can be converted between a Java object and a JSON string. JSONArray represents a JSON array, which can be used to convert a Java set (a collection element can be an object) to a JSON string. 1. Conversion between a Java object and a JSON string:

Student stu = new Student (1005, "James", "Wuhan", "zs.jpg ");

JSONObject jsonObject = new JSONObject (stu );

JsonObject. toString () is the string output: {"sid": 1005, "name": "Zhang San", "addr": "Wuhan", "pic ": "zs.jpg"} is parsed as an object:

JSONObject jsonObject = new JSONObject (str );

 

Iterator <String> iterator = jsonObject. keys ();

Student stu = new Student ();

 

While (iterator. hasNext ()){

// You can use java reflection to write generic methods.

String key = iterator. next ();

If (key. equals ("sid ")){

Stu. setSid (jsonObject. getInt (key ));

}

 

If (key. equals ("name ")){

Stu. setName (jsonObject. getString (key ));

}

 

If (key. equals ("sid ")){

Stu. setAddr (jsonObject. getString (key ));

}

 

If (key. equals ("sid ")){

Stu. setPic (jsonObject. getString (key ));

}

}

Method 2: Student stu = new Student (1005, "Zhang San", "Wuhan", "zs.jpg ");

JSONObject jsonObject = new JSONObject ();

JsonObject. put ("zs", stu );

 

Similarly, jsonObject. toString () is the string output {"zs": "{sid: 1005, name: James, addr: Wuhan, pic: zs.jpg }"}. Note that if Student does not overwrite the toString () method, the input is: {"zs": "org. itair. domain. Student @ 2666e815 "}

 

 

 

 

2. Conversion between a set and a JSON string 1. Conversion:

Student s1 = new Student (1001, "zhangsan", "yichang", "111 ");

Student s2 = new Student (1002, "Jack", "Beijing", "222 ");

 

List <Student> stus = new ArrayList <Student> ();

 

Stus. add (s1 );

Stus. add (s2 );

 

JSONArray array = new JSONArray (stus );

 

The array string is: [{"sid": 1001, "name": "zhangsan", "addr": "yichang", "pic": "111 "}, {"sid": 1002, "name": "Jack", "addr": "Beijing", "pic": "222"}]

Resolution: JSONArray jsonArray = new JSONArray ("json string to be parsed ");

For (int I = 0; I <jsonArray. length (); I ++ ){

JSONObject jsonObject2 = jsonArray. getJSONObject (I );

// Parse it into a specific object operation, new object, and get the value from jsonObject2 for loading

Iterator <String> iterator = jsonObject2.keys ();

While (iterator. hasNext ()){

String key = iterator. next ();

Object value = jsonObject2.get (key );

Map. put (key, value );

}

}

Method 2: conversion: Student s1 = new Student (1001, "zhangsan", "yichang", "111 ");

Student s2 = new Student (1002, "Jack", "Beijing", "222 ");

 

List <Student> stus = new ArrayList <Student> ();

 

Stus. add (s1 );

Stus. add (s2 );

 

JSONObject jsonObject = new JSONObject ();

JsonObject. put ("students", stus );

JsonObject string: {"students": [{"sid": 1001, "name": "zhangsan", "addr": "yichang", "pic ": "111" },{ "sid": 1002, "name": "Jack", "addr": "Beijing", "pic": "222"}]}

Resolution: The preceding conversion method is to put the set object in JSONObject. JSONObject jsonObject = new JSONObject ("json string to be parsed ");

JSONArray jsonArray = jsonObject. getJSONArray ("key"); // The key is the students put above.

The subsequent code is the same as above.

 

In Android projects, the Json String is generally not parsed into a collection of objects, but mostly into a List <Map <String, Object>

 

List <Map <String, Object> list = new ArrayList <Map <String, Object> ();

 

For (int I = 0; I <jsonArray. length (); I ++ ){

JSONObject jsonObject2 = jsonArray. getJSONObject (I );

Map <String, Object> map = new HashMap <String, Object> ();

Iterator <String> iterator = jsonObject2.keys ();

While (iterator. hasNext ()){

String key = iterator. next ();

Object value = jsonObject2.get (key );

Map. put (key, value );

}

 

List. add (map );

 

}

 

It is more convenient to parse it into an object set, or use Gson and fastjson.

 

3. GsontoJson (Object) converts the Object into a json String fromJson (String, Object) and converts the json String into an Object.

Related Article

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.