各位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" |
另外解法 請參考
文章標籤
全站熱搜

請問大大我想要抓取政府open data請問網址要放哪邊接取
這邊需要用到一些連線的機制, 你可以參考“OkHttpClient 教學”的用法 參考網址如下: http://jc7003.pixnet.net/blog/post/306816565-okhttpclient-%E6%95%99%E5%AD%B8 "call.enqueue(callback);" 此行主要是取回server的回應 回傳值會呼叫callback中的onResponse “public void onResponse(Call call, Response response)” 再用response.body().toString 取值
在 2. JSONObject中包一個JSONObject 資訊 的 "其解析方式如下" 裡, JSONObject info = jsonObject.getJSONObject("info"); String skill = jsonObject.getString("skill"); String web_url = jsonObject.getString("web_url"); 應改為 JSONObject info = jsonObject.getJSONObject("info"); String skill = info.getString("skill"); String web_url = info.getString("web_url"); 才有東西唷!
沒想到這錯誤存在了兩年多... 已修正了, 太感謝您了
想問JSONObject jsonObject = new JSONObject(jsonText); 的(jsonText)部分怎麼來的 谷哥很多地方json教學但內容通常new JSONObject()框框內一開始就放 有一個用jsonarray包數個jsonobject的php檔要放入android studio去解析 參考了下http://kangkaipro.blogspot.com/2018/06/android-studio-json.html 的教學 對方用的php裡一開始就是物件result中的一個陣列results {result: { , , results [ {},{},{}]} JSONArray data = jsonObject.getJSONObject("result").getJSONArray("results"); 我的陣列沒有命名什麼 內容直接就是[{物件},{物件},{物件}]也不知道框框該怎麼命名
jsonText 就是您的 json String, 也就是範例中的 “{"id":"1", ... }” 對於php api而言,一定會有個results 這個是api回來的結果,看他物件的型態,基本上來說一定可以轉成String。 (也當然可以直接轉成JSONObject,我在Kotlin中也有這樣寫,比較省事。) 再放入 JSONObject 或 JSONArray中做物件生成,方便資料的讀取。
你好~ 最近做專題遇到一個抓取氣象局JSON資料的問題,方便跟你留個連絡方式線上請教你嗎!
沒有問題,請給我你的聯絡方式吧。
*****