下面將以一個例子來講解如何使用 JSONObject 去解析JSON
第一步:準備工作
這裏我用的是一個百度地圖定位的鏈接,打開就可以看到這次要解析的JSON了ps:地址後面的 ak 可以直接用,如果失效的話可以去百度地圖開放平臺申請一個
如果覺得亂,推薦用這個網站整好看些
JSON在線工具:http://www.sojson.com/
第二步:明確要獲取的數據的父子關係
假如我們要獲取的是下圖劃紅線部分的文字,下面文章出現的鍵名表示要獲取的數據的標籤名,如addr;
其中藍色部分的“result”和“pois”表示父級關係
第三步:代碼實現
package com.example.administrator.httpdemo.HttpURLConnectionFile;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.example.administrator.httpdemo.R;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//這裏用百度地圖的一個api來做例子(地址後面的ak可以直接用,如果失效的話可以去申請一個)
sendReques("http://api.map.baidu.com/geocoder/v2/?location=39.934,116.329" +
"&output=json&pois=1&ak=4hWL4Pn8THzPoCGSlS10GNdexou9cZif");
}
/* 我這裏是用HttpURLConnection來發送請求,獲取json數據
你也可以直接把json轉成String類型再來解析
這裏就不細講得到json的過程
*/
private void sendReques(final String strURL) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(strURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(2000);
connection.setReadTimeout(5000);
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
//開始解析 (傳入String類型的json)
pareJSONwithJSONObject(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
//解析方法
private void pareJSONwithJSONObject(String jsonData) {
try {
//將服務器返回的數據傳入到JSONObject對象中
JSONObject object = new JSONObject(jsonData);
//將object中的 result{} 作爲一個對象傳入到 object2 中
JSONObject object2 = object.getJSONObject("result");
//由於(劃藍線部分)pois[]它是一個數組的形式,
//我們需要調用getJSONArray()方法並傳給 JSONArray
JSONArray jsonArray = object2.getJSONArray("pois");
//遍歷數組
for (int i = 0; i < jsonArray.length(); i++) {
//通過角標獲取"數組"的對象
JSONObject jsonObject = jsonArray.getJSONObject(i);
//通過調用getString("劃紅線部分的鍵名")方法獲取想要的數據
String str_addr = jsonObject.getString("addr");
//打開Logcat,查看是是否得到想要獲取的數據
Log.d("logcity", str_addr);
}
} catch (Exception e) {
}
}
}
概括:
一般情況下,如果你想獲取的數據不包含在數組中,只要一直嵌套,最後調用getString()方法獲取就行了,代碼如下:
/*假如只嵌套了兩層*/
JSONObject object = new JSONObject("json數據源");
JSONObject object2 = object.getJSONObject("鍵名的父級的父級");
JSONObject object3 = object2.getJSONObject("鍵名的父級");
String string = jsonObject3.getString("鍵名");
如果想獲取的數據包含在數組中,可以先遍歷再取值;
步驟:遍歷數組—>調用getJSONObject(index)方法—>再調用getString("鍵名")方法即可;
其中,index表示角標。代碼如下:
JSONObject jsonObject = jsonArray.getJSONObject(index);
String string = jsonObject.getString("鍵名");