各位android 大大好~!
小弟今天要說明android中如和解析JSON
在此先說明一下,何謂JSON格式
簡單來說 JSON是一種key value pair
以此組合為JSONObject, 多組相同格式的JSONObject則組成JSONArray
JSONObject 格式為"{ }" 大括弧包起來; JSONArray則是"[ ]" 中括弧
以下為簡單的json範本
1. 一個簡單JSONObject
{
"id":"1", "name":"歐文", "title":"json 解析",
"tag":"android json", "info":"1234567890"
}
其解析方式如下
//建立一個JSONObject並帶入JSON格式文字,getString(String key)取出欄位的數值
try{
JSONObject jsonObject = new JSONObject(jsonText);
String name = jsonObject.getString("name");
String title = jsonObject.getString("title");
String tag = jsonObject.getString("tag");
String info = jsonObject.getString("info");
}
catch(JSONException e) {
e.printStackTrace();
}
2. JSONObject中包一個JSONObject 資訊
{
"id":"1", "name":"歐文", "title":"json 解析", "tag":"android json",
"info":{"skill":"java", "web_url":"http://jc7003.pixnet.net/blog"}
}
其解析方式如下
try{
//建立一個JSONObject並帶入JSON格式文字,getString(String key)取出欄位的數值
JSONObject jsonObject = new JSONObject(jsonText);
String name = jsonObject.getString("name");
String title = jsonObject.getString("title");
String tag = jsonObject.getString("tag");
// 因為info為JSONObject, 所以使用getJSONObject(String key)抓取出來
JSONObject info = jsonObject.getJSONObject("info");
String skill = info.getString("skill");
String web_url = info.getString("web_url");
}
catch(JSONException e) {
e.printStackTrace();
}
3. JSONArray範例
[
{"id":"1", "title":"json object 1", "tag":"android json", "info":"1234567890" },
{"id":"2", "title":"json object 2", "tag":"android json", "info":"abcdefg" },
{"id":"3", "title":"json object 3", "tag":"android json", "info":"qwerty" }
]
其解析方式如下
try{
//建立一個JSONArray並帶入JSON格式文字,getString(String key)取出欄位的數值
JSONArray array = new JSONArray(jsonText);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String title = jsonObject.getString("title");
String tag = jsonObject.getString("tag");
String info = jsonObject.getString("info");
Log.d("TAG", "title:" + title + ", tag:" + tag + ", info:" + info);
}
}
catch(JSONException e) {
e.printStackTrace();
}
其結果為
|
"title: json object 1, tag: android json, info: 1234567890" "title: json object 2, tag: android json, info: abcdefg" "title: json object 3, tag: android json, info: qwerty" |
4. JSONObject中包一個JSONArray範例
{
"id":"1", "name":"歐文", "title":"json 解析", "tag":"android json",
"info":[
{"id":"1", "title":"json object 1", "tag":"android json", "info":"1234567890" },
{"id":"2", "title":"json object 2", "tag":"android json", "info":"abcdefg" },
{"id":"3", "title":"json object 3", "tag":"android json", "info":"qwerty" }
]
}
其解析方式如下
try{
//建立一個JSONObject並帶入JSON格式文字,getString(String key)取出欄位的數值
JSONObject jsonObject = new JSONObject(jsonText);
String name = jsonObject.getString("name");
String title = jsonObject.getString("title");
String tag = jsonObject.getString("tag");
JSONArray array = jsonObject.getJSONArray("info");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String title = jsonObject.getString("title");
String tag = jsonObject.getString("tag");
String info = jsonObject.getString("info");
Log.d("TAG", "title:" + title + ", tag:" + tag + ", info:" + info);
}
}
catch(JSONException e) {
e.printStackTrace();
}
其結果為
|
"title: json object 1, tag: android json, info: 1234567890" "title: json object 2, tag: android json, info: abcdefg" "title: json object 3, tag: android json, info: qwerty" |
另外解法 請參考
