2016-09-17 87 views
1

我想解析這個:https://s3.amazonaws.com/dolartoday/data.json爲Android應用程序顯示美元兌玻利維亞非官方匯率。解析沒有陣列的JSON文件

我正在使用本教程:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/,之前我已成功地使用過,但是之前使用的API包含一個JSON數組和子對象。然而,這個有12個帶字符串的JSON對象。沒有方括號可見。

的部分(我認爲)我有麻煩是:

if (jsonStr != null) { 
    try { 
    JSONObject jsonObj = new JSONObject(jsonStr); 

    // Getting JSON Array node 
    JSONArray contacts = jsonObj.getJSONArray("contacts"); 

    // looping through All Contacts 
    for (int i = 0; i < contacts.length(); i++) { 
     JSONObject c = contacts.getJSONObject(i); 

     String id = c.getString("id"); 
     String name = c.getString("name"); 
     String email = c.getString("email"); 
     String address = c.getString("address"); 
     String gender = c.getString("gender"); 

     // Phone node is JSON Object 
     JSONObject phone = c.getJSONObject("phone"); 
     String mobile = phone.getString("mobile"); 
     String home = phone.getString("home"); 
     String office = phone.getString("office"); 
    } 
    } catch (final JSONException e) { 
     Log.e(TAG, "Json parsing error: " + e.getMessage()); 
     runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(getApplicationContext(), 
          "Json parsing error: " + e.getMessage(), 
          Toast.LENGTH_LONG) 
          .show(); 
     } 
     }); 
    } 
} 

我不是專家,因爲我想學習編程我自己,我還是很新的。但是,我做了一些更改,包括刪除ListView,並將代碼調整爲新的JSON。

if (jsonStr != null) { 
    try { 
    JSONObject jsonObj = new JSONObject(jsonStr); 

    // Getting JSON Object node 
    JSONObject c = jsonObj.getJSONObject("USD"); 
    JSONObject d = jsonObj.getJSONObject("EUR"); 

    String usdtrans = c.getString("dolartoday"); 
    String usdreal = c.getString("efectivo_real"); 
    String usddicom = c.getString("sicad2"); 
    String eurtrans = d.getString("dolartoday"); 
    String eurreal = d.getString("efectivo_real"); 
    String eurdicom = d.getString("sicad2"); 
    } 
    } catch (final JSONException e) { 
     Log.e(TAG, "Json parsing error: " + e.getMessage()); 
     runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(getApplicationContext(), 
         "Json parsing error: " + e.getMessage(), 
         Toast.LENGTH_LONG) 
         .show(); 
     } 
     }); 
    } 
} 

該代碼無法編譯,Android Studio檢測到"unhandled exception: org.json.JSONException"。我能做些什麼來改變這一點?

任何幫助,將不勝感激。我的其他代碼也有其他問題,但我想我可以在完成這一步後將其排除。

+0

您之前'抓(最終JSONException E)在你的第二個代碼'了不必要的大括號。 –

+0

json:''dolartoday「:1026.13', code:'String usdtrans = c.getString(」dolartoday「);' 該json的值不是String。它是雙倍的。如果你getString,這將是錯誤的。 –

+0

不,它不會。 double值將被轉換爲字符串並返回 –

回答

0

使用org.json庫類時,如果JSON響應由方括號「[」組成,那麼我們需要將該元素作爲JSONArray獲取,否則將元素作爲JSONObject獲取。

try { 
     JSONObject jsonObject = new JSONObject(jsonString); 

     JSONObject antibloqueo = jsonObject.getJSONObject("_antibloqueo"); 
     JSONObject labels = jsonObject.getJSONObject("_labels"); 
     JSONObject timestamp = jsonObject.getJSONObject("_timestamp"); 
     JSONObject USD = jsonObject.getJSONObject("USD"); 
     JSONObject EUR = jsonObject.getJSONObject("EUR"); 
     JSONObject COL = jsonObject.getJSONObject("COL"); 
     JSONObject GOLD = jsonObject.getJSONObject("GOLD"); 
     JSONObject USDVEF = jsonObject.getJSONObject("USDVEF"); 
     JSONObject USDCOL = jsonObject.getJSONObject("USDCOL"); 
     JSONObject EURUSD = jsonObject.getJSONObject("EURUSD"); 
     JSONObject BCV = jsonObject.getJSONObject("BCV"); 
     JSONObject MISC = jsonObject.getJSONObject("MISC"); 


     //To get Values From antibloqueo 
     String mobile = antibloqueo.getString("mobile"); 
     String video = antibloqueo.getString("video"); 


     //To get Values From labels 
     String a = labels.getString("DOLARTODAY"); 
     String a1 = labels.getString("a1"); 


    } catch (JSONException e) { 
     Log.e("TAG", "Json parsing error: " + e.getMessage()); 
    } 

注: 雖然與規模較小的項目,則可以使用org.json庫作爲JSON轉換器。對於較大的項目,你必須用到其他庫一樣GSON /傑克遜

https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

+0

太好了,我已經整理出了那部分內容,但是現在我遇到了一些以前沒有的錯誤。 我得到一個沒有,如果錯誤正確的新代碼下。不知道那裏會出現什麼問題,因爲我檢查過,是的,有一個if。 而且我還在onPostExecute變量上「無法解析符號結果」。不知道我在哪裏可以粘貼代碼 –

+0

使用http://www.paste.org粘貼代碼 – Pandiarajan

+0

對不起,花了很長時間,這裏是:http://www.paste.org/81498 我基於本教程中的代碼:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/,它將代碼解析爲ListView。我不想要ListView,因爲我想將代碼解析爲兩個簡單的TextView,所以我試圖刪除它。這就是我認爲出現錯誤的原因。謝謝你的幫助! –