Android 之 json数据的解析(jsonReader)

json数据的解析相对而言,仍是比较容易的,实现的代码也十分简单。这里用的是jsonReade方法来进行json数据解析。java

 

1.在解析以前,你们须要知道什么是json数据。android

 

json数据存储的对象是无序的“名称/值”对的集合。和其余的数据存储方式相比,json数据的可读性,可扩展性,编码难度,解码难度都有必定的优点。在json数据中,json

 

对于一个对象:数组

1)一个对象以“{”(左括号)开始,“}”(右括号)结束。安全

2)每一个“名称”后跟一个“:”(冒号);测试

3)“‘名称/值’ 对”之间使用“,”(逗号)分隔。编码

 

对于一个数组:spa

 

1)一个数组以“[”(左中括号)开始,“]”(右中括号)结束。code

2)值之间使用“,”(逗号)分隔。对象

 

下面是android官方给出的一组json数据示例:

[
   {
     "id": 912345678901,
     "text": "How do I read JSON on Android?",
     "geo": null,
     "user": {
       "name": "android_newb",
       "followers_count": 41
      } 
   },
   {
     "id": 912345678902,
     "text": "@android_newb just use android.util.JsonReader!",
     "geo": [50.454722, -104.606667],
     "user": {
       "name": "jesse",
       "followers_count": 2
     }
   }
 ]

 

在代码中,若是直接定义json数据,须要在代码中对 使用 \ 转义。上面json在代码中的形式为:(在java代码中,建立一段json数据,“ 符号须要转义)

private String jsonDate = "["
            + "{\"id\": 912345678901,"
            + "\"text\":\"How do I read JSON on Android?\","
            + "\"geo\":null,"
            + "\"user\":{\"name\":\"android_newb\",\"followers_count\":41}},"
            + "{\"id\": 912345678902,"
            + "\"text\":\"@android_newb just use android.util.JsonReader!\","
            + "\"geo\":[50.454722,-104.606667],"
            + "\"user\":{\"name\":\"jesse\",\"followers_count\":2}}"
            + "]";

 

1. 使用JsonReader方法解析Json数据对象,你须要建立一个JsonReader对象.

 

2.而后使用beginArray()来开始解析 [ 左边的第一个数组。

 

3.再使用beginObject()来开始解析数组{中的第一个对象。

 

4.对于直接的数据能够直接获得解析到的数据,但对于在json中嵌套了数组的数据,须要在写一个解析方法。

 

5.在解析完成后,别忘用endArray(),endObject()来关闭解析。

 

package com.mecury.jsontest;
 
import java.io.IOException;
import java.io.StringReader;
import android.util.JsonReader;
import android.util.JsonToken;
public class JsonUtils {
 
    public void parseJson(String jsonDate) throws IOException{
            //建立JsonReader对象
            JsonReader jsReader = new JsonReader(new StringReader(jsonDate));
            jsReader.beginArray();
            while (jsReader.hasNext()) {
                readMessage(jsReader);
            }
            jsReader.endArray();
   
    }
   
        public void readMessage(JsonReader jsReader) throws IOException{
            jsReader.beginObject();
            while(jsReader.hasNext()){
                String tagName = jsReader.nextName();
                if (tagName.equals("id")) {
                    System.out.println("name:"+jsReader.nextLong());
                }else if (tagName.equals("text")) {
                    System.out.println("text:"+jsReader.nextString());
                }else if (tagName.equals("geo") && jsReader.peek()!=JsonToken.NULL) {
                    readDoubleArray(jsReader);
                }else if (tagName.equals("user")) {
                    readUser(jsReader);
                }else {
                    //跳过当前值
                    jsReader.skipValue();
                    System.out.println("skip======>");
                }
            }
            jsReader.endObject();
        }
        //解析geo中的数据
        public void readDoubleArray(JsonReader jsReader) throws IOException{
            jsReader.beginArray();
            while(jsReader.hasNext()){
                System.out.println(jsReader.nextDouble());
            }
            jsReader.endArray();
        }
        //因为读取user中的数据
        public void readUser(JsonReader jsReader) throws IOException{
        String userName = null;
        int followsCount = -1;
        jsReader.beginObject();
        while (jsReader.hasNext()) {
            String tagName = jsReader.nextName();
            if (tagName.equals("name")) {
                userName = jsReader.nextString();
                System.out.println("user_name:"+ userName);
            }else if (tagName.equals("followers_count")) {
                followsCount = jsReader.nextInt();
                System.out.println("followers_count:"+followsCount);
            }
        }
        jsReader.endObject();
    }
}

 

对上面的内容解析的输出:

11-22 06:59:52.441: I/System.out(5329): name:912345678901
11-22 06:59:52.441: I/System.out(5329): text:How do I read JSON on Android?
11-22 06:59:52.461: I/System.out(5329): skip======>
11-22 06:59:52.461: I/System.out(5329): user_name:android_newb
11-22 06:59:52.471: I/System.out(5329): followers_count:41
11-22 06:59:52.481: I/System.out(5329): name:912345678902
11-22 06:59:52.491: I/System.out(5329): text:@android_newb just use android.util.JsonReader!
11-22 06:59:52.500: I/System.out(5329): 50.454722
11-22 06:59:52.500: I/System.out(5329): -104.606667
11-22 06:59:52.510: I/System.out(5329): user_name:jesse
11-22 06:59:52.510: I/System.out(5329): followers_count:2

 

以上!另外对APP进行在线全方位的安全性、兼容性测试,我都会用这个:www.ineice.com

相关文章
相关标签/搜索