一、JSON介绍
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C,C++,C#,Java,JavaScript,Perl,Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。
1.JSON数据是一系列键值对的集合;
2.JSON已经被大多数开发人员接受,在网络数据传输当中应用非常广泛;
3.JSON相对于XML解析方便一些。
JSON和XML对比:
1.JSON和XML的数据可读性基本相同;
2.JSON和XML同样拥有丰富的解析手段;
3.JSON相对于XML来讲,传递同等价值的信息时数据体积小;
4.JSON与JavaScript交换更加方便;
5.JSON对数据的描述性相对较差。
二、JSON数据格式特点
JSON数据格式例子:
[
{
"name" : "allorry",
"age" : 25
"address" :
{
"city" : "BeiJing",
"postCode" : 123456
}
},
{
"name" : "bauble",
"age" : 25
"address" :
{
"city" : "BeiJing",
"postCode" : 123456
}
}
]
{}代表了一个JSON对象,里面是一个个键值对,而[]代表了是一个JSON数组,里面是多个JSON对象。
三、使用Gson解析JSON数据
Android API中可以使用JsonReader进行JSON数据的解析,但是JsonReader是针对3.0版以上的SDK,所以并不适合现在使用。可以使用google的Gson进行JSON数据的解析。其实JsonReader就是将Google-gson添加到了Android SDK当中,只不过在3.0版本以下没有。使用方法和效果是一样的。Gson是自包含的,不依赖其他类库。
要想使用Google-gson需要下载gson的jar包:
http://code.google.com/p/google-gson/
例子程序:
首先需要在buildpath当中添加gson.jar。
JSONActivity.Java
package com.android.activity;
import java.io.IOException;
import java.io.StringReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.gson.stream.JsonReader;
public class JSONActivity extends Activity {
private Button button = null;
private String jsonData = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
jsonData = "[{\"name\" : \"allorry\",\"age\" : 25}
,{\"name\" : \"bauble\",\"age\" : 25}]";
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
try {
parseJSON(jsonData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void parseJSON(String jsonData) throws IOException{
//如果需要解析JSON数据,要生成一个JsonReader对象
JsonReader reader = new JsonReader(new StringReader(jsonData));
//表示现在已经开始解析数组了,解析数组-解析对象-解析键值对-解析对象结束-解析数组结束
reader.beginArray();
//判断有没有对象
while(reader.hasNext()){
reader.beginObject();
//判断有没有键值对
while(reader.hasNext()){
String tagName = reader.nextName();
if(tagName.equals("name")){
System.out.println("name - "+reader.nextString());
}else if(tagName.equals("age")){
System.out.println("age - "+reader.nextInt());
}
}
reader.endObject();
}
reader.endArray();
}
}
运行结果:
另外,使用Gson可以很方便的将json对象转换为我们自己定义的对象:
User.java
package com.android.activity;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
JSONActivity.java
package com.android.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.gson.Gson;
public class JSONActivity extends Activity {
private Button button = null;
private String jsonData = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
jsonData = "{\"name\" : \"allorry\",\"age\" : 25}";
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{
public void onClick(View v) {
Gson gson = new Gson();
//得到对象,参数1:json数据对象,参数2:希望转换成的类对象
User user = gson.fromJson(jsonData, User.class);
System.out.println("name - "+user.getName());
System.out.println("age - "+user.getAge());
}
}
}
输出结果:
如果想要JSON数组转换成多个的User对象,可以使用TypeToken:
private void parseUserFromJson(String jsonData){
Type listType = new TypeToken<LinkedList<User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> userList = gson.fromJson(jsonData, listType);
for(Iterator<User> i = userList.iterator();i.hasNext();){
User user = (User)i.next();
System.out.println("name - "+user.getName());
System.out.println("age - "+user.getAge());
}
}
注意:此时jsonData传进来的是一个JSON数组。