Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到JsonObject_Java_Json - Fatal编程技术网

Java 找不到JsonObject

Java 找不到JsonObject,java,json,Java,Json,我通过以下代码调用api System.out.println("its running"); String realTimeData = "https://api.coindesk.com/v1/bpi/currentprice.json"; HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macin

我通过以下代码调用api

System.out.println("its running");
String realTimeData = "https://api.coindesk.com/v1/bpi/currentprice.json";
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
conn.setRequestMethod("GET");

String inputline;
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null){
    System.out.println(response.append(inputLine));
}
in.close();
其中
response
输出以下Json输出

{"time":{"updated":"Mar 4, 2018 18:53:00 UTC","updatedISO":"2018-03-04T18:53:00+00:00","updateduk":"Mar 4, 2018 at 18:53 GMT"},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{"USD":{"code":"USD","symbol":"$","rate":"11,333.4625","description":"United States Dollar","rate_float":11333.4625},"GBP":{"code":"GBP","symbol":"£","rate":"8,208.4869","description":"British Pound Sterling","rate_float":8208.4869},"EUR":{"code":"EUR","symbol":"€","rate":"9,200.3915","description":"Euro","rate_float":9200.3915}}}
我正在尝试通过键获取数据,使用此代码

JSONObject json = new JSONObject(response);
String rate = json.getString("time");
System.out.println("rate was "+rate);

每当我运行上述代码段时,我都会得到一个
JsonObject{“time”}未找到
。我的结论是,要么我不明白在上面的json格式中什么是“键”。或者Cointdesk吐出的
响应
在我的代码中没有被视为Json

您试图将字段
time
作为字符串获取。当它不是。这是一个JSONObject

还要注意,您没有使用正确的构造函数

以下是您应该做的:

JSONObject json = new JSONObject (response.toString ());

JSONObject oTime = json.getJSONObject ("time");

System.out.println("rate was " + oTime);

您试图将字段
时间
作为字符串获取。当它不是。这是一个JSONObject

还要注意,您没有使用正确的构造函数

以下是您应该做的:

JSONObject json = new JSONObject (response.toString ());

JSONObject oTime = json.getJSONObject ("time");

System.out.println("rate was " + oTime);
“时间”是另一个JSONObject的关键。 如果要从“时间”对象检索美元汇率,请尝试以下操作:

JSONObject json = new JSONObject(response);
JSONObject bpi = json.getJSONObject("bpi");
JSONObject usd = bpi.getJSONOBJECT("USD");
String rate = usd.getString("rate");
System.out.println("rate was "+rate);
进一步资料: 您可以在以下位置轻松检查JSON结构: 希望我能帮助你

时间是另一个JSONObject的关键。 如果要从“时间”对象检索美元汇率,请尝试以下操作:

JSONObject json = new JSONObject(response);
JSONObject bpi = json.getJSONObject("bpi");
JSONObject usd = bpi.getJSONOBJECT("USD");
String rate = usd.getString("rate");
System.out.println("rate was "+rate);
进一步资料: 您可以在以下位置轻松检查JSON结构:
希望我能帮助你

你需要发帖。现在,我们甚至不知道您使用的是哪个JSON包
JSONObject
实际上不是一个很少见的类名。@lexicore人们在使用
JSONObject
时通常引用的是
org.json
。您需要发布。现在,我们甚至不知道您使用的是哪个JSON包
JSONObject
实际上并不是一个很少见的类名。@lexicore人们在使用
JSONObject
时通常指的是
org.json
。我觉得自己太蠢了,我想把
time
当作字符串和整数什么的,但我没想到把它当成json。@Sahil很高兴我能帮上忙!另外,请看我关于构造函数的编辑。我觉得自己太蠢了,我想把
时间
作为字符串和整数之类的东西,但我没有想到把它作为Json。@Sahil很高兴我能帮上忙!另外,请参阅我对构造函数的编辑。