2014-01-20 54 views
0

例JSON頁 http://maps.googleapis.com/maps/api/geocode/json?latlng=51.155455,-0.165058&sensor=true的Android不知道如何來引用值JSON文件(JSON解析)

@SuppressLint("NewApi") 
public void readAndParseJSON(String in) { 
    try { 
     JSONObject reader = new JSONObject(in); 
     // This works and returns address 
     JSONArray resultArry = reader.getJSONArray("results"); 
     String Address = resultArry.getJSONObject(1).getString("formatted_address").toString(); 
     Log.e("Address", Address); 
     // Trying to get PostCode on code below - this is not working (log says no value at address components) 
     JSONArray postCodeArray = reader.getJSONArray("address_components"); 
     String postCode = postCodeArray.getJSONObject(1).getString("long_name").toString(); 
     Log.e("PostCode", postCode); 

此代碼正確返回的地址。我如何獲得address_components內的郵政編碼long_name?

解決方案 我必須得到每個數組,然後獲取郵政編碼值。 我使用的值是7,因爲這是具有存儲在「long_name」字段中的郵編的JSONObject。

JSONObject readerJsonObject = new JSONObject(in); 
readerJsonObject.getJSONArray("results"); 
JSONArray resultsJsonArray = readerJsonObject.getJSONArray("results"); 
JSONArray postCodeJsonArray = resultsJsonArray.getJSONObject(0).getJSONArray("address_components"); 
String postCodeString = postCodeJsonArray.getJSONObject(7).getString("long_name").toString();   
Log.e("TAG", postCodeString); 

希望有所幫助。

回答

0
reader.getJSONObject(1).getJSONArray("address_components"); 
+0

感謝您的評論。我解決了這個問題。請參閱我的答案以獲得更新。 – user3013243

0

您的問題是resultsJSONArray包含幾個孩子組成的子JSONObject"address_components""formatted_address""geometry""types"result數組實際上包含許多這樣的對象,但我們現在只關注第一個孩子。

仔細看看你的代碼。使用此行:

JSONArray resultArry = reader.getJSONArray("results"); 

您將收到整個results。後來,你又再次調用同樣的方法:

JSONArray postCodeArray = reader.getJSONArray("address_components"); 

但是你要求從讀者,在這裏我不指望你會發現任何一個"address_components"(在已經閱讀之前的整個結果。)您應該使用之前已有的JSONArray,因爲它已經包含了整個結果。

試着這麼做:

JSONObject addressComponents = resultArry.getJSONObject(1).getJSONObject("address_components"); 
String postCode = addressComponents.getString("long_name"); 

注:我不知道爲什麼你挑出的JSONObject#1(而不是0,這是第一個,或它們中的任何其他一個)和我也不確定你爲什麼命名String postCode。所以如果我誤解了你的意圖,我很抱歉。

+0

嗨,我挑出JsonObject 1,因爲它以我想要的格式返回地址。 我將字符串postCode命名爲我試圖從JSON保存郵編部分。 地址部分工作,我從JSON文件返回地址, 我只是想單獨獲取郵政編碼以存儲到郵政編碼字符串。 – user3013243

+0

我試過了你的代碼,我得到了類型爲org.json.JSONArray的address_components上的錯誤 無法轉換爲JSONObject at org.json.JSON.typeMismatch(JSON.java:100) – user3013243

+0

是的,看起來像你最終得到它。 JSON對於[]和{}很棘手,我似乎總是讓它們混淆起來。 – sigmabeta

0

很難找到錯誤...因爲一切都很好看。當你創建json.put(「address_components」,something)時,問題可能會存在。

所以我的建議是把一個斷點在該行

JSONArray postCodeArray = reader.getJSONArray("address_components"); 

Ø在logcat中顯示JSON

Log.d("Simple", reader.toString()); 

因此,在本網頁將JSON粘貼到瀏覽更靚

http://jsonviewer.stack.hu/

並檢查所有密鑰是否存儲好吧。

+0

感謝您的建議。我解決了它,請查看我的答案以獲得更新。 – user3013243

+0

好的。好消息! –

0

解決方案 需要獲取每個數組,然後獲取郵政編碼值。使用值7,因爲這是具有存儲在「long_name」字段中的郵編的JSONObject。

JSONObject readerJsonObject = new JSONObject(in); 
readerJsonObject.getJSONArray("results"); 
JSONArray resultsJsonArray = readerJsonObject.getJSONArray("results"); 
JSONArray postCodeJsonArray = resultsJsonArray.getJSONObject(0).getJSONArray("address_components"); 
String postCodeString = postCodeJsonArray.getJSONObject(7).getString("long_name").toString();   
Log.e("TAG", postCodeString); 

希望有所幫助。

+0

如果這是解決方案,則應將其標記爲答案,以便隨後的每個人都清楚。 – Lovis