1. 什么是 JSON
JSON概念很简单,JSON 是一种轻量级的数据格式,他基于 javascript语法的子集,即数组和对象表示。由于使用的是 javascript 语法,因此JSON 定义可以包含在javascript文件中,对其的访问无需通过基于 XML 的语言来额外解析。不过在使用 JSON 之前,很重要的一点是理解 javascript中数组及对象字面量的特殊语法。
1.1 数组字面量
数组字面量,是用一对方括号括起一组用逗号隔开的 javascript 值,例如:
var aNames=["hello", 12, true , null];
1.2 对象字面量
对象字面量,是通过两个花括号来定义的。在花括号内可以放置任意数量的“名称-值”对,定义格 式字符串值”。除了最后一行外,每个“名称-值”对后必须有一个逗号(这与Perl 中的联合数组的定义有些类似)。例如:
var oCar = {
"color": "red",
"doors" : 4,
"paidFor" : true
};
1.3 混合字面量
我们可以混用对象和数组字面量,来创建一个对象数组,或一个包含数组的对象。例如:
{comments:[
{
id:1,
author:"someone1",
url:"http://someone1.x2design.net",
content:"hello"
},
{
id:2,
author:"someone2",
url:"http://someone2.x2design.net",
content:"hello"
},
{
id:3,
author:"someone3",
url:"http://someone3.x2design.net",
content:"hello"
}
]};
1、JSON是用来进行服务器和客户端数据传输的
2、JSON数据是一系列键值对的集合
3、JSON已经被大多数开发人员接受,在网络数据的传输当中应用非常广泛
JSON PK XML哪个更好?
JSON相对于XML来讲解析稍微方便一些。
JSON和XML的数据可读性基本相同
JSON和XML同样拥有丰富的解析手段
JSON相对于XML来讲,数据体积小
JSON与JavaScript的交互更加方便
JSON对数据的描述性相对于XML较差
一个JSON的例子:
-
- "name":"Michael";
-
- "address":
-
- {
-
- "city":"Beijing",
-
- "street":"Chaoyang Road",
-
- "postcode":100025
-
- }
-
- }
{
"name":"Michael";
"address":
{
"city":"Beijing",
"street":"Chaoyang Road",
"postcode":100025
}
}
说明:JSON官方网站:http://www.json.org/
Google JSON类库:http://code.google.com/p/google-gson/
看下面的例子:
Activity类:
- public class MainActivity extends Activity {
- private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
- private Button button ;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.buttonId);
- button.setOnClickListener(new ButtonListener());
- }
-
- private class ButtonListener implements OnClickListener {
-
- public void onClick(View v) {
- JsonUtils jsonUtils = new JsonUtils();
- jsonUtils.parseJson(jsonData);
- }
-
- }
- }
public class MainActivity extends Activity {
private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
private Button button ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new ButtonListener());
}
private class ButtonListener implements OnClickListener {
public void onClick(View v) {
JsonUtils jsonUtils = new JsonUtils();
jsonUtils.parseJson(jsonData);
}
}
}
工具类:
- public class JsonUtils {
- public void parseJson(String jsonData){
- try{
-
- 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();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
public class JsonUtils {
public void parseJson(String jsonData){
try{
//如果需要解析JSON数据,首要要生成一个JsonReader对象
JsonReader reader = new JsonReader(new StringReader(jsonData));
//开始解析数组
reader.beginArray();
//判断是否下一个JSON对象
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();
}
catch(Exception e){
e.printStackTrace();
}
}
}
一个JSON:[{"name":"zhangsan","age":20},{"name":"lisi","age":30}]
步骤:
1>开始解析数组
2>开始解析对象
3>解析键值对
4>解析键值对
5>解析对象结束
6>开始解析对象
7>解析键值对
8>解析键值对
9>解析对象结束
10>解析数组结束
只要理解上面的解析流程,学习JSON应该不是很难。
在看下面的这个例子:
User.java类
- 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;
- }
- }
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;
}
}
JsonUtils类:
- public class JsonUtils {
-
- public void parseUserFromJson(String jsonData){
- Gson gson = new Gson();
- User user = gson.fromJson(jsonData, User.class);
- System.out.println("name--->" + user.getName());
- System.out.println("age---->" + user.getAge());
-
- }
- }
public class JsonUtils {
public void parseUserFromJson(String jsonData){
Gson gson = new Gson();
User user = gson.fromJson(jsonData, User.class);
System.out.println("name--->" + user.getName());
System.out.println("age---->" + user.getAge());
}
}
MainActivity.java类:
- public class MainActivity extends Activity {
- private Button button = null;
- private String jsonData = "{\"name\":\"Michael\",\"age\":20}";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.buttonId);
- button.setOnClickListener(new ButtonListener());
- }
-
- private class ButtonListener implements OnClickListener{
-
- public void onClick(View v) {
- JsonUtils jsonUtils = new JsonUtils();
- jsonUtils.parseUserFromJson(jsonData);
- }
-
- }
- }
public class MainActivity extends Activity {
private Button button = null;
private String jsonData = "{\"name\":\"Michael\",\"age\":20}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new ButtonListener());
}
private class ButtonListener implements OnClickListener{
public void onClick(View v) {
JsonUtils jsonUtils = new JsonUtils();
jsonUtils.parseUserFromJson(jsonData);
}
}
}
JsonUtils类:
- public class JsonUtils {
-
- public void parseUserFromJson(String jsonData){
-
-
-
-
-
- Gson gson = new Gson();
-
-
-
-
-
-
-
-
-
- User user = gson.fromJson(jsonData, User.class);
- System.out.println("name--->" + user.getName());
- System.out.println("age---->" + user.getAge());
-
- }
- }
public class JsonUtils {
public void parseUserFromJson(String jsonData){
/*
* This is the main class for using Gson.
* Gson is typically used by first constructing a Gson instance
* and then invoking toJson(Object) or fromJson(String, Class) methods on it
*/
Gson gson = new Gson();
/*public <T> T fromJson(String json,
* Type typeOfT)
* throws JsonSyntaxException
* This method deserializes the specified Json into an object of the specified type
* json - the string from which the object is to be deserialized
* typeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
*/
User user = gson.fromJson(jsonData, User.class);
System.out.println("name--->" + user.getName());
System.out.println("age---->" + user.getAge());
}
}
在往下看这个例子:
MainActivity.java和User.java同上,JsonUtils.java代码如下:
- public class JsonUtils {
-
- public void parseUserFromJson(String jsonData){
- Type listType = new TypeToken<LinkedList<User>>(){}.getType();
- Gson gson = new Gson();
- LinkedList<User> users = gson.fromJson(jsonData, listType);
- for (Iterator iterator = users.iterator(); iterator.hasNext();) {
- User user = (User) iterator.next();
- System.out.println("name--->" + user.getName());
- System.out.println("age---->" + user.getAge());
- }
-
- }
- }
public class JsonUtils {
public void parseUserFromJson(String jsonData){
Type listType = new TypeToken<LinkedList<User>>(){}.getType();
Gson gson = new Gson();
LinkedList<User> users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println("name--->" + user.getName());
System.out.println("age---->" + user.getAge());
}
}
}
上面的代码让你一步一步深入学习JSON各种操作方法!