2017-10-13 86 views
0

我正在使用一個API提供的加密比較。我需要從此JSON對象獲得符號價格如何獲取和解析這個JSON對象到Android textview

{ 
    "Response": "Success", 
    "Message": "Do not take life too seriously. You will never get out of it alive.", 
    "Data": [ 
    { 
     "Symbol": "USD", 
     "Price": 5660.94, 
     "Open24Hour": 5155.13, 
     "LastUpdateTS": 1507885905, 
     "Volume24Hours": 222438.875, 
     "Volume24HoursTo": 1214073220 
    }, 
    { 
     "Symbol": "EUR", 
     "Price": 4757.16, 
     "Open24Hour": 4318.19, 
     "LastUpdateTS": 1507885905, 
     "Volume24Hours": 26488.4023, 
     "Volume24HoursTo": 120264888 
    } 
    ], 
    "Type": 100 
} 
+1

那你試試? –

+1

我的朋友,我建議你至少嘗試一下。這是一項您可以輕鬆實現Google搜索的任務。讓我給你一個提示:[Gson](https://github.com/google/gson) –

回答

2

我會建議您將數據存儲到List

所以,初始化2 List

List<String> symbol = new ArrayList<String>(); 
List<String> price = new ArrayList<String>(); 

那麼在這裏你可以存儲數據

try { 

    JSONObject json = new JSONObject(response); 
    JSONArray jArray = json.getJSONArray("Data"); 
    for (int i = 0; i < jsonArray.length(); i++) { 
     JSONObject object = jsonArray.getJSONObject(i); 
     symbol.add(object.getString("Symbol")); 
     price.add(object.getString("Price")); 

    } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 

這可能對你有幫助

+0

謝謝。這是我最好的答案。 – emeUshiwu

+0

很高興這個作品,請點擊打勾接受這個答案@emeUshiwu – UltimateDevil

+0

我該如何接受它? – emeUshiwu

1

怎麼辦?

  • 如果你在你的代碼滿足{},您可以使用JSONObject解析它。

  • 如果您在代碼中遇到[],則可以使用JSONArray來解析它。

  • 如果您在代碼中遇到[],則可以使用for loop來獲取其中的值。您的代碼中應該使用try catch

試試這個。

try { 
     JSONObject jsonObject = new JSONObject(response); 
     String Response = jsonObject.optString("Response"); 
     JSONArray Data = jsonObject.optJSONArray("Data"); 
     for (int i = 0; i < Data.length(); i++) { 
      JSONObject jo = Data.optJSONObject(i); 
      String Symbol = jo.optString("Symbol"); 
      String Price = jo.optString("Price"); 
      String Open24Hour = jo.optString("Open24Hour"); 
      String LastUpdateTS = jo.optString("LastUpdateTS"); 
      String Volume24Hours = jo.optString("Volume24Hours"); 
      String Volume24HoursTo = jo.optString("Volume24HoursTo"); 
     } 
} catch (JSONException e) { 
     e.printStackTrace(); 
} 
+0

謝謝你的回答它幫助了我 – emeUshiwu

0

檢查了這一點

try { 

      JSONObject objresponse=new JSONObject("{\"Response\":\"Success\",\"Message\":\"Do not take life too seriously. You will never get out of it alive.\",\"Data\":[{\"Symbol\":\"USD\",\"Price\":5660.94,\"Open24Hour\":5155.13,\"LastUpdateTS\":1507885905,\"Volume24Hours\":222438.875,\"Volume24HoursTo\":1.21407322E+09},{\"Symbol\":\"EUR\",\"Price\":4757.16,\"Open24Hour\":4318.19,\"LastUpdateTS\":1507885905,\"Volume24Hours\":26488.4023,\"Volume24HoursTo\":120264888.0}],\"Type\":100}"); 
      JSONArray arrayData=objresponse.getJSONArray("Data"); 
      for (int i=0;i<arrayData.length();i++){ 
       JSONObject obj=arrayData.getJSONObject(i); 
       String symbol=obj.getString("Symbol"); 
       float price=(float)obj.getLong("Price"); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }