- 比起 XML,JSON 的主要优势在于它的体积更小,在网络上传输的时
候可以更省流量。但缺点在于,它的语义性较差,看起来不如XML 直观。 - 一段 JSON 格式数据如下:
[{"id":"5","version":"5.5","name":"Angry Birds"},
{"id":"6","version":"7.0","name":"Clash of Clans"},
{"id":"7","version":"3.5","name":"Hey Day"}]
一、使用 JSONObject
- 单条 JSON:
{"city":"昆山","cityid":"101190404","temp1":"21℃","temp2":"9℃",
"weather":"多云转小雨","img1":"d1.gif","img2":"n7.gif","ptime":"11:00"}
try {
JSONObject jsonObject=new JSONObject(jsonData);
String cityName=jsonObject.getString("city");
String temp1 = jsonObject.getString("temp1");
String temp2 = jsonObject.getString("temp2");
String weatherDesp = jsonObject.getString("weather");
String publishTime = jsonObject.getString("ptime");
String result=cityName+":"+temp1+"——"+temp2+" "+weatherDesp+" at:"+publishTime+"\n";
textView.setText(result);
} catch (JSONException e) {
e.printStackTrace();
}
- JSON 数组:
[{"id":"5","version":"5.5","name":"Angry Birds"},
{"id":"6","version":"7.0","name":"Clash of Clans"},
{"id":"7","version":"3.5","name":"Hey Day"}]
parseJSONWithJSONObject(response);
......
......
private void parseJSONWithJSONObject(String jsonData) {
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.d("MainActivity", "id is " + id);
Log.d("MainActivity", "name is " + name);
Log.d("MainActivity", "version is " + version);
}
} catch (Exception e) {
e.printStackTrace();
}
}
- 首先是将服务器返回的数据传入到了一个 JSONArray 对象中。
- 然后循环遍历这个 JSONArray,从中取出的每一个元素都是一个 JSONObject 对象,每个 JSONObject 对象中又会包含 id、name 和 version 这些数据。
- 只需要调用** getString() **方法将这些数据取出。
二、使用 GSON
dependencies {
......
compile 'com.google.code.gson:gson:2.7'
......
}
GSON 可以将一段 JSON 格式的字符串自动映射成一个对象,从而不需要再手动去编写代码进行解析了。
先创建要存放数据的类,包含对应 JSON 数据的变量。
使用 Gson 的 fromJson 方法,将数据存储到一个具体对象中。
单条 JSON:
{"city":"昆山","cityid":"101190404","temp1":"21℃","temp2":"9℃",
"weather":"多云转小雨","img1":"d1.gif","img2":"n7.gif","ptime":"11:00"}
{
......
Gson gson=new Gson();
WeatherInfo weatherInfo=gson.fromJson(jsonData3,WeatherInfo.class);
String result=weatherInfo.getCity()+":"+weatherInfo.getTemp1()+"——"+weatherInfo.getTemp2()
+" "+weatherInfo.getWeather()+" at:"+weatherInfo.getPtime()+"\n";
textView.setText(result);
......
}
......
private class WeatherInfo{
private String city;
private String temp1;
private String temp2;
private String weather;
private String ptime;
public String getCity() {
return city;
}
......
public void setPtime(String ptime) {
this.ptime = ptime;
}
}
- JSON 数组:
{
......
Gson gson=new Gson();
List<Book> list=gson.fromJson(jsonData2, new TypeToken<List<Book>>(){}.getType());
for(Book b:list){
result=b.getId()+":"+b.getVersion()+":"+b.getName()+"\n";
}
textView.setText(result);
......
}
......
private class Book{
private String id;
private String version;
private String name;
public String getId() {
return id;
}
......
public void setName(String name) {
this.name = name;
}
}
- 需要借助 TypeToken 将期望解析成的数据类型传入到 fromJson() 方法中。
不能直接使用gson.fromJson(jsonData2, List<Book>.class)
,这是因为对于Java来说List<Book>
和List<String>
这俩个的字节码文件都是List.class
(泛型擦除),所以要用TypeToken
来实现对泛型的支持。
(或者也可以使用gson.fromJson(jsonData2, Book[].class)
) - 然后将数据存入一个 List,里面包括一组对象,分别对应每条 JSON 数据。
- 然后循环遍历这个 List,对相应对象相应数据做操作。
- 复合嵌套的 JSON 数据:
{
"a":"100",
"b":[
{"b1":"1b1","b2":"1b2"},
{"b1":"2b1","b2":"2b2"}
],
"c":{"c1":"c1","c2":"c2"}
}
可以利用内部类,一次性完成解析,将数据都存入 JsonBean 里:
private class JsonBean{
private String a;
private B[] b;
private C c;
public C getC() {
return c;
}
......
public void setA(String a) {
this.a = a;
}
private class B{
private String b1;
private String b2;
public String getB2() {
return b2;
}
......
public void setB1(String b1) {
this.b1 = b1;
}
}
private class C{
private String c1;
private String c2;
public String getC1() {
return c1;
}
......
public void setC2(String c2) {
this.c2 = c2;
}
}
}
Gson gson=new Gson();
JsonBean jsonBean=gson.fromJson(jsonData4,JsonBean.class);
result=result+"\n"+jsonBean.getA()+"_"+jsonBean.getB()[0].getB1()+"_"+
jsonBean.getB()[0].getB2()+"_"+jsonBean.getB()[1].getB1()+"_"+
jsonBean.getB()[1].getB2()+"_"+jsonBean.getC().getC1()+"_"+
jsonBean.getC().getC2();
B[ ] 也可以用 List<B> 代替。