0

I am using an API provided by CryptoCompare. I need to get Symbol and Price from this JSON object.

{
  "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
}
Bishan
  • 15,211
  • 52
  • 164
  • 258
emeishiwu
  • 27
  • 9

3 Answers3

2

I will suggest you to store your data to List

So, Initialize two List

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

then here you can store data

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();
}

This may be help you

UltimateDevil
  • 2,807
  • 2
  • 18
  • 31
  • please answer this too https://stackoverflow.com/questions/46730373/how-can-i-get-strings-from-a-complex-json-object-without-a-json-array – emeishiwu Oct 13 '17 at 12:51
1

How to do it ?

  • If you meet {} in your code , you can use JSONObject to parse it .

  • If you meet [] in your code , you can use JSONArray to parse it .

  • And if you meet [] in your code , you can use for loop to get value in it .

  • And you should use try catch in your code .

Try this .

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();
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

Check this out

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();
        }
Chandan kushwaha
  • 941
  • 6
  • 26