-5

Lets assume i get a json like that, how can i get the GBP value in this json thanks

    {
   "time":{
      "updated":"Jul 13, 2016 10:14:00 UTC",
      "updatedISO":"2016-07-13T10:14:00+00:00",
      "updateduk":"Jul 13, 2016 at 11:14 BST"
   },
   "disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
   "bpi":{
      "USD":{
         "code":"USD",
         "symbol":"$",
         "rate":"667.0690",
         "description":"United States Dollar",
         "rate_float":667.069
      },
      "GBP":{
         "code":"GBP",
         "symbol":"£",
         "rate":"502.3230",
         "description":"British Pound Sterling",
         "rate_float":502.323
      },
      "EUR":{
         "code":"EUR",
         "symbol":"€",
         "rate":"602.4634",
         "description":"Euro",
         "rate_float":602.4634
      }
   }
}
Onkar Nene
  • 1,359
  • 1
  • 17
  • 23
jb4
  • 3
  • 5

5 Answers5

0

You need to do this way:

String jsonString = "{\n" +
                "\t\"time\": {\n" +
                "\t\t\"updated\": \"Jul 13, 2016 10:14:00 UTC\",\n" +
                "\t\t\"updatedISO\": \"2016-07-13T10:14:00+00:00\",\n" +
                "\t\t\"updateduk\": \"Jul 13, 2016 at 11:14 BST\"\n" +
                "\t},\n" +
                "\t\"disclaimer\": \"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org\",\n" +
                "\t\"bpi\": {\n" +
                "\t\t\"USD\": {\n" +
                "\t\t\t\"code\": \"USD\",\n" +
                "\t\t\t\"symbol\": \"$\",\n" +
                "\t\t\t\"rate\": \"667.0690\",\n" +
                "\t\t\t\"description\": \"United States Dollar\",\n" +
                "\t\t\t\"rate_float\": 667.069\n" +
                "\t\t},\n" +
                "\t\t\"GBP\": {\n" +
                "\t\t\t\"code\": \"GBP\",\n" +
                "\t\t\t\"symbol\": \"£\",\n" +
                "\t\t\t\"rate\": \"502.3230\",\n" +
                "\t\t\t\"description\": \"British Pound Sterling\",\n" +
                "\t\t\t\"rate_float\": 502.323\n" +
                "\t\t},\n" +
                "\t\t\"EUR\": {\n" +
                "\t\t\t\"code\": \"EUR\",\n" +
                "\t\t\t\"symbol\": \"€\",\n" +
                "\t\t\t\"rate\": \"602.4634\",\n" +
                "\t\t\t\"description\": \"Euro\",\n" +
                "\t\t\t\"rate_float\": 602.4634\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";

        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONObject jGbpObject = jsonObject.getJSONObject("bpi").getJSONObject("bpi");
            Log.i("GBP", jGbpObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

Hope this would help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

JSONObject provides access for a number of different data types, including nested JSONObjects and JSONArrays, using JSONObject.getJSONObject(String), JSONObject.getJSONArray(String).

you'd need to do something like this:

Parse the JSON data with respective URl and then -

JSONObject mainJsonObject = getJSONfromURL(URL);
JSONObject bpi = mainJsonObject.getJSONObject("bpi");
JSONObject gbp = bpi.getJSONObject("GBP");
String code = gbp.getString("code");
Onkar Nene
  • 1,359
  • 1
  • 17
  • 23
0

Let's say "myjsonObject" contains your JsonObject. Then you can get values as follows :

JsonObject bpiObject = myjsonObject.getJSONObject("bpi");

JsonObject gbpObject = ibpObject.getJSONObject("GBP");

You can then get individual values from "gbpObject".

Deepak Singh
  • 114
  • 6
0

To parse this, you need to understand the json first. In JSON we have JSON objects, JSON arrays as wrapping components.

{ //this right here is the top level object which wraps your whole data
"time": {//this right here is the inner object called time
    "updated": "Jul 13, 2016 10:14:00 UTC",
    "updatedISO": "2016-07-13T10:14:00+00:00",//the object may contain another object, array and may as contain a entity as string
    "updateduk": "Jul 13, 2016 at 11:14 BST"
},
"disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"bpi": {//this objects contains three more objects 
    "USD": {
        "code": "USD",
        "symbol": "$",
        "rate": "667.0690",
        "description": "United States Dollar",
        "rate_float": 667.069
    },
    "GBP": {
        "code": "GBP",
        "symbol": "£",
        "rate": "502.3230",
        "description": "British Pound Sterling",
        "rate_float": 502.323
    },
    "EUR": {
        "code": "EUR",
        "symbol": "€",
        "rate": "602.4634",
        "description": "Euro",
        "rate_float": 602.4634
    }
}
}

Your JSON contains a top-level object which wraps two objects "time","bpi" and a string "description". Get the top level object and wrapped data as:

 try {
        JSONObject toplevel_jsonObject = new JSONObject(YOUR_JSON_STRING);
        JSONObject time_jsonObject=toplevel_jsonObject.getJSONObject("time");
        String updated=time_jsonObject.getString("update");
        ...
        //get the other objects and string in the same fashion
    }
    catch (JSONException e)
    {

    }
karan vs
  • 3,044
  • 4
  • 19
  • 26
0
JsonObject mResponseJson=new JsonObject(response);
JsonObject mBpiObj=mResponseJson.getJSONObject("bpi");
JsonObject mGBPObj=mBpiObj.getJSONObject("GBP");

String code=mGBPObj.getString("code");
String symbol=mGBPObj.getString("symbol");
String rate=mGBPObj.getString("rate");
String description=mGBPObj.getString("description");
String rate_float=mGBPObj.getString("rate_float");
Anantha Babu
  • 216
  • 2
  • 14