我怎样才能获得JSON值

时间:2016-07-13 10:18:48

标签: java android json

让我们假设我得到了一个像这样的json,我怎样才能在这个json中得到GBP值谢谢

    {
   "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
      }
   }
}

5 个答案:

答案 0 :(得分:0)

你需要这样做:

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

希望这会对你有所帮助。

答案 1 :(得分:0)

new DefaultContractResolver();使用JSONObjectJSONObjects提供对多种不同数据类型的访问权限,包括嵌套的JSONArraysJSONObject.getJSONObject(String)

你需要做这样的事情:

使用相应的URl解析JSON数据,然后 -

JSONObject.getJSONArray(String)

答案 2 :(得分:0)

假设“ myjsonObject ”包含您的JsonObject。然后您可以获得如下值:

JsonObject bpiObject = myjsonObject.getJSONObject(“bpi”);

JsonObject gbpObject = ibpObject.getJSONObject(“GBP”);

然后,您可以从“gbpObject”获取单个值。

答案 3 :(得分:0)

要解析这个,你需要先了解json。在JSON中,我们有JSON对象,JSON数组作为包装组件。

{ //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
    }
}
}

您的JSON包含一个顶级对象,它包含两个对象“time”,“bpi”和一个字符串“description”。 获取顶级对象和包装数据:

 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)
    {

    }

答案 4 :(得分: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");