ALDRIDGE 2011-10-12
1。什么是JSON
就是现在网络上比较流行的数据交换格式,在网络上比较通用的数据交换格式呢就是XML,在二个异构系统当中,进行数据交换,XMl是最好的选择,最近几年呢,这个JSON以体积小解码方便
谷歌提供的API将JSON数据转换为JAVA数据,
1.JSON:JavaScriptObjectNotatoin
JSON这种数据来源于javascrip,
2。JSON数据是一系列键值对的集合
JSONVSXML
A:二者的数据可读性基本相同
B:二者同样拥有丰富的解析手段
C:JSON的体积更小
D:JSON与JS交互更加方便
E:JSON对数据的描述性相对差
一个JSON例子:
{ "name":"Michael" "address": { "city":"Biejing" "street":"Chaoyang Road" } }
中括号代表可以放很多个JSON对象
layout布局文件中
<Button android:id="@+id/buttonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试" />
mainAcitvity文件:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestJson01Activity extends Activity { /** Called when the activity is first created. */ private String jsonDate = "[{\"name\":\"Machael\",\"age\":21},{\"name\":Mike\",\"age\":30}]"; private Button button = null; @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 { @Override public void onClick(View v) { // TODO Auto-generated method stub JsonUtils jsonUtils = new JsonUtils(); jsonUtils.parseJson(jsonDate); } } }
JsonUtils.java
import java.io.StringReader; import com.google.gson.stream.JsonReader; public class JsonUtils { public void parseJson(String jsonDate) { try { //如果需要解析Json数据,首先要生成一个JsonReader对象 JsonReader jsonReader = new JsonReader(new StringReader(jsonDate)); jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); while (jsonReader.hasNext()) { String tagName = jsonReader.nextName(); if (tagName.equals("name")) { System.out.println("name--->"+jsonReader.nextString()); } else if (tagName.equals("age")) { System.out.println("age--->"+jsonReader.nextInt()); } } jsonReader.endObject(); } jsonReader.endArray(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
Json解析的第二种用法
1、创建一个Activity文件,
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestJson02Activity extends Activity { /** Called when the activity is first created. */ private String jsonDate = "{\"name\":\"Machael\",\"age\":21}"; private Button button = null; @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 { @Override public void onClick(View v) { // TODO Auto-generated method stub JsonUtils jsonUtils = new JsonUtils(); jsonUtils.parseUserFromJson(jsonDate); } } }
创建一个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; } }
JsonUtils.java文件
public class JsonUtils { public void parseUserFromJson(String jsonDate) { //创建Gson对象 Gson gson = new Gson(); //将JsonDate转化为User对象 User user = gson.fromJson(jsonDate, User.class); System.out.println("name--->"+user.getName()); System.out.println("age--->"+user.getAge()); } }
当是多个对象时,将JsonUtils.java文件改下
public class JsonUtils { public void parseUserFromJson(String jsonDate) { Type listType = new TypeToken<LinkedList<User>>() { }.getType(); //创建Gson对象 Gson gson = new Gson(); LinkedList<User> users = gson.fromJson(jsonDate, listType); for (Iterator iterator = users.iterator(); iterator.hasNext();) { User user = (User) iterator.next(); System.out.println("name--->"+user.getName()); System.out.println("name--->"+user.getAge()); } } }