我需要解析JSON。不幸的是,我无法像here中描述的那样通过JSON字符串获取名称和速率。此外,this one对我没有帮助。
{"usd":
{"code":"USD",
"alphaCode":"USD",
"numericCode":"840",
"name":"U.S. Dollar",
"rate":1.0857128644692,
"date":"Mon, 21 Dec 2015 12:00:01 GMT"},
"gbp":
{"code":"GBP",
"alphaCode":"GBP",
"numericCode":"826",
"name":"U.K. Pound Sterling",
"rate":0.72830809326194,
"date":"Mon, 21 Dec 2015 12:00:01 GMT"},
"cad":
{"code":"CAD",
"alphaCode":"CAD",
"numericCode":"124",
"name":"Canadian Dollar",
"rate":1.5123600265482,
"date":"Mon, 21 Dec 2015 12:00:01 GMT"}
}
结果:
“美元。”“1.0857128644692”
“Canadian Dollar”“1.5123600265482”
答案 0 :(得分:4)
尝试此解决方案
try {
JSONObject MainJsonObject = new JSONObject("Your Sting input data");
Iterator keyNames = MainJsonObject.keys();
while (keyNames.hasNext()) {
String keyname = (String) keyNames.next();
JSONObject jsonObject = MainJsonObject.getJSONObject(keyname);
// parse this this jsonObjectas you required
}
}catch (Exception e) {
e.printStackTrace();
}