您的位置:首页 > 移动开发 > Android开发

Android 之 json数据的解析(jsonReader)

2015-11-27 10:10 441 查看
json数据的解析相对而言,还是比较容易的,实现的代码也十分简单。这里用的是jsonReade方法来进行json数据解析。

1.在解析之前,大家需要知道什么是json数据。

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

对于一个对象:
(1)一个对象以“{”(左括号)开始,“}”(右括号)结束。
(2)每个“名称”后跟一个“:”(冒号);
(3)“‘名称/值’对”之间使用“,”(逗号)分隔。

对于一个数组:

(1)一个数组以“[”(左中括号)开始,“]”(右中括号)结束。
(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 useandroid.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 onAndroid?\","
+ "\"geo\":null,"
+"\"user\":{\"name\":\"android_newb\",\"followers_count\":41}},"
+ "{\"id\": 912345678902,"
+ "\"text\":\"@android_newb just useandroid.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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android