Android Json解析 - 初步
JSON與XML一樣都是數(shù)據(jù)格式,JSON的數(shù)據(jù)格式比XML更小。這里主講JSON。
JSON是JavsScript對(duì)象符號(hào)。
下面是一個(gè)簡(jiǎn)單的JSON格式
{"name":"zhangsan","age":22,"address":"太原"}
這就是一個(gè)對(duì)象,里面的值key是冒號(hào)前面的類容,value是冒號(hào)后面的類容。
JSON還可以有數(shù)組的形式,如下:
[{"name":"zhangsan","age":22,"address":"太原"},
{"name":"lisi","age":20,"address":"黃岡"},
{"name":"wangwu","age":21,"address":"武漢"}]
在中括號(hào)里全都是對(duì)象,這種對(duì)象的個(gè)數(shù)沒(méi)限制,對(duì)象里是鍵值對(duì)。
在JSON對(duì)象中還可以有對(duì)象,如下:
{
"name":"zhangsan","age":22,
"address":{"country":"中國(guó)","province":"山西","city":"太原"}
}
這就是說(shuō)address key是一個(gè)對(duì)象。
這就是JSON的基本形式。
首先下載解析JSON需要的Jar包 下載。這是Google提供解析JSON的開源的Json Jar包。
1.最基礎(chǔ)的解析
這種解析式最普遍的,根據(jù)JSON的格式來(lái)解析,我來(lái)解析如下的JSON數(shù)據(jù):
{"name":"zhangsan","age":22,"address":"太原"}
首先
String s="{"name":"zhangsan","age":22,"address":"太原"}";
這里使用反斜杠來(lái)使用雙引號(hào)。
JsonReader reader=new JsonReader(new StringReader(s));
聲明一個(gè)JsonReader對(duì)象,將String s傳入。
try {
JsonReader reader=new JsonReader(new StringReader(data));
reader.beginObject();
while(reader.hasNext())
{
String key=reader.nextName();
if("name".equals(key))
{
System.out.println(reader.nextString());
}
if("age".equals(key))
{
System.out.println(reader.nextInt());
}
if("address".equals(key))
{
System.out.println(reader.nextString());
}
}
reader.endObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
結(jié)果如下:
開始解析的時(shí)候調(diào)用reader.beginObject();結(jié)束的時(shí)候調(diào)用reader.endObject().應(yīng)為一個(gè)對(duì)象里可能有很多的鍵值對(duì),所以要使用while循環(huán),當(dāng)對(duì)象里的鍵值對(duì)都循環(huán)完了的話reader.hasNext()方法返回false。reader.nextXXX()獲得你所要的值,XXX為類型。
2.解析數(shù)組
下面解析如下的Json數(shù)據(jù):
[{"name":"zhangsan","age":22,"address":"太原"},
{"name":"lisi","age":20,"address":"黃岡"},
{"name":"wangwu","age":21,"address":"武漢"}]
如下代碼:
try {
JsonReader reader=new JsonReader(new StringReader(data));
reader.beginArray();
while(reader.hasNext())
{
reader.beginObject();
while(reader.hasNext())
{
String key=reader.nextName();
if("name".equals(key))
{
System.out.println(reader.nextString());
}
if("age".equals(key))
{
System.out.println(reader.nextInt());
}
if("address".equals(key))
{
System.out.println(reader.nextString());
}
}
reader.endObject();
}
reader.endArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
根據(jù)這個(gè)Json的數(shù)據(jù)結(jié)構(gòu),我先解析數(shù)組,所以開始就reader.beginArray();當(dāng)然還要reader.endArray();同樣使用reader.hasNext()來(lái)判斷數(shù)組是否都解析完了。
下面是效果圖:
3.包含對(duì)象的對(duì)象解析
下面我解析如下的Json數(shù)據(jù):
{
"name":"zhangsan","age":22,
"address":{"country":"中國(guó)","province":"山西","city":"太原"}
}
廢話不多說(shuō)先來(lái)代碼:
try {
JsonReader reader=new JsonReader(new StringReader(data));
reader.beginObject();
while(reader.hasNext())
{
String key=reader.nextName();
if("name".equals(key))
{
System.out.println(reader.nextString());
}
if("address".equals(key))
{
reader.beginObject();
while(reader.hasNext())
{
String theKey=reader.nextName();[!--empirenews.page--]
if("country".equals(theKey))
{
System.out.println(reader.nextString());
}
if("province".equals(theKey))
{
System.out.println(reader.nextString());
}
if("city".equals(theKey))
{
System.out.println(reader.nextString());
}
}
reader.endObject();
}
}
reader.endObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
都給解析出來(lái)了。
1.解析一個(gè)對(duì)象
可以直接從Json中將隊(duì)形解析出來(lái),這個(gè)Json必須是一個(gè)Json對(duì)象,如下:
{"name":"zhangsan","age":22,"address":"太原"}
它只是一個(gè)對(duì)象。
我新建一個(gè)類,生成get和set方法,如下:
package com.zhycheng;
publicclass Human {
String name,address;
int age;
public String getName() {
return name;
}
publicvoid setName(String name) {
this.name = name;
}
publicint getAge() {
return age;
}
publicvoid setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
publicvoid setAddress(String address) {
this.address = address;
}
}
然后使用如下代碼直接解析
String s="{"name":"zhangsan","age":22,"address":"太原"}";
Gson g=new Gson();
Human h=g.fromJson(s, Human.class);
System.out.println(h.getName()+";"+h.getAge()+";"+h.getAddress());
2.解析數(shù)組
下面解析如下的Json數(shù)據(jù):
[{"name":"zhangsan","age":22,"address":"太原"},
{"name":"lisi","age":20,"address":"黃岡"},
{"name":"wangwu","age":21,"address":"武漢"}]
代碼如下:
String s="[{"name":"zhangsan","age":22,"address":"太原"},{"name":"lisi","age":20,"address":"黃岡"},{"name":"wangwu","age":21,"address":"武漢"}]";
Type type=new TypeToken>(){}.getType();
Gson g=new Gson();
LinkedList ll=g.fromJson(s, type);
for(int i=0;i
{
Human h=(Human)ll.get(i);
System.out.println(h.getName()+","+h.getAge()+","+h.getAddress());
}
在這里要注意:
1.Json里的數(shù)據(jù)時(shí)數(shù)組,數(shù)組里是對(duì)象
2.泛形一定要用,否則出錯(cuò)
項(xiàng)目工程下載。