在我的项目中,我从HTTP响应中获取以下JSON。
{"base":"USD","date":"2015-04-24","rates":{"AUD":1.2827,"BGN":1.8069,"BRL":2.9733,"CAD":1.2119,"CHF":0.9551,"CNY":6.1948,"CZK":25.364,"DKK":6.8927,"GBP":0.6614,"HKD":7.75,"HRK":7.0284,"HUF":278.53,"IDR":12954.0,"ILS":3.9244,"INR":63.563,"JPY":119.51,"KRW":1078.25,"MXN":15.358,"MYR":3.5741,"NOK":7.8298,"NZD":1.3216,"PHP":44.281,"PLN":3.7076,"RON":4.08,"RUB":51.215,"SEK":8.6674,"SGD":1.3375,"THB":32.55,"TRY":2.7314,"ZAR":12.182,"EUR":0.9239}}
我想从上面的json获得“BGN”。怎么弄它。
答案 0 :(得分:0)
此
涉及多个步骤首先:创建一个jsonObject
JSONObject jObject = new JSONObject(yourJSON_String);
第二步:根据您的情况获取所需的对象或数组
obj = getJSONObject("rates")
第三:获取必需的字符串
obj.getString("BGN");
答案 1 :(得分:0)
JSONObject responseJson = new JSONObject("<your-string">);
JSONObject ratesJson = responseJson.getJSONObject("rates");
double rate = ratesJson.getDouble("BGN");
要清楚地查看JSON
对象,请使用PrettyJson。复制粘贴JSON
字符串,它将显示嵌套对象(如果有),以便清楚地理解响应。
答案 2 :(得分:0)
假设您正在使用JSONObject,并且您的根文档被调用&#34; doc&#34;,
JSONObject rates = doc.getJSONObject("rates")
可以获得费率部分:
{"AUD":1.2827,"BGN":1.8069,"BRL":2.9733,"CAD":1.2119,"CHF":0.9551,"CNY":6.1948,"CZK":25.364,"DKK":6.8927,"GBP":0.6614,"HKD":7.75,"HRK":7.0284,"HUF":278.53,"IDR":12954.0,"ILS":3.9244,"INR":63.563,"JPY":119.51,"KRW":1078.25,"MXN":15.358,"MYR":3.5741,"NOK":7.8298,"NZD":1.3216,"PHP":44.281,"PLN":3.7076,"RON":4.08,"RUB":51.215,"SEK":8.6674,"SGD":1.3375,"THB":32.55,"TRY":2.7314,"ZAR":12.182,"EUR":0.9239}
然后,您可以使用
简单地从此对象获取BGN值 String bgn = rates.getString("BGN")