在Json(Java)中导航

在Json(Java)中导航,java,android,json,android-studio,Java,Android,Json,Android Studio,我不熟悉JSON,所以如果我使用了错误的术语,很抱歉 所以我有一个网址: final String PRICE_TRY_URL = "https://api.coindesk.com/v1/bpi/currentprice/try.json"; 它会返回这样的结果: 格式化JSON数据 { "time":{ }, "disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD).

我不熟悉JSON,所以如果我使用了错误的术语,很抱歉

所以我有一个网址:

 final String PRICE_TRY_URL = "https://api.coindesk.com/v1/bpi/currentprice/try.json";
它会返回这样的结果:

格式化JSON数据

{  
   "time":{  },
   "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",
         "rate":"6,911.7500",
         "description":"United States Dollar",
         "rate_float":6911.75
      },
      "TRY":{  
         "code":"TRY",
         "rate":"35,738.0058",
         "description":"Turkish Lira",
         "rate_float":35738.0058
      }
   }
}
我想做的就是达到TRY的速度。 我用下面的代码得到数据

public void doNetworking(){
        AsyncHttpClient client=new AsyncHttpClient();
        client.get(PRICE_TRY_URL, new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        Log.d("BitcoinTracker","Succes in doNetworking");
                        // byte[] responseBody can be parsed to a json object.
                        parseJson(responseBody);
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        Log.e("BitcoinTracker", "Fail " + error.toString());
                    }
                }
        );
以下是我的方法:

public void parseJson(byte[] responseBody){
        try {
            JSONObject bitcoinJson  =new JSONObject(new String(responseBody));
            String currency=  bitcoinJson.getString("bpi");
            Log.d("bitcoinmanager",currency);

        } catch (JSONException e) {
            Log.d("BitcoinPrice","BitcoinPriceManager onSucces converting json from byte[] failed.");
            e.printStackTrace();
        }
    }
正如您在上面所看到的,我使用以下语句:

String currency=  bitcoinJson.getString("bpi");
有了这句话,我无法达到我的目的点,那就是尝试的速度。如何在JSON格式的文本中导航


注意:我添加了获取JSON数据部分,以确保我的问题清楚,希望不要太多。

如果您使用的是Android,无需使用任何外部工具:

JSONObject bitcoinJson = new JSONObject(responseBody);
JSONObject bpi = bitcoinJson.getJSONObject("bpi");
JSONObject tr = bpi.getJSONObject("TRY");
String rate = tr.getString("rate");
在问题被标记为Android之前,使用
org.json.simple
库的原始答案:

JSONObject bitcoinJson = (JSONObject)new JSONParser().parse(new String(responseBody));
JSONObject bpi = (JSONObject)bitcoinJson.get("bpi");
JSONObject tr = (JSONObject)bpi.get("TRY");
String rate = (String)tr.get("rate");

请注意,与其构造一个字符串来传递给
JSONParser
,不如让它直接访问
读取器

您可以将此代码与Gson库一起使用:

client.get(PRICE_TRY_URL, new TextHttpResponseHandler() {

    @Override
    public void onSuccess(int statusCode, Header[] headers, String res) {
            // called when response HTTP status is "200 OK"
            JsonParser jsonParser = new JsonParser();
            JsonObject jsonObject = (JsonObject) jsonParser.parse(responseBodyString);
            JsonObject bpi = jsonObject.get("bpi").getAsJsonObject();
            JsonObject tryObject = bpi.get("TRY").getAsJsonObject();
            String rate = tryObject.get("rate").getAsString();
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
    }   
  }
);

这里有一些例子

String currency=bitcoinJson.getString(“bpi”)
此语句将返回一个对象,该对象必须按键
USD
TRY
,然后提取
TRY
,最后您将获得其速率。@jackjay真的吗
getString
返回一个对象,而不是
String
?我想你的意思是
getJSONObject
。那是因为
bpi
后面的条目不是字符串。为什么要使用getString()?@Andreas是的。我试图导入/实现(不确定哪个术语正确)org.json.simple 1.1.1,但在我的gradle文件中发生了错误。“json定义的类与Android现在提供的类冲突”@Gilthoniel您没有将问题标记为Android:-)现在添加了一个Android示例。对不起,我太糊涂了。我将立即添加标签。谢谢你的回答:——)@Gilthoniel没问题。