解析没有数组的JSON
问题描述:
我需要解析JSON。不幸的是,我无法按照here中描述的通常方法从JSON字符串获取名称和费率。另外,this one不帮助我。解析没有数组的JSON
{"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”
“加拿大元” “1.5123600265482”
答
尝试此解决方案
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();
}
请参见本我的回答[链接](http://stackoverflow.com/questions/34397172/android-issue-with-jsonarray-parsing)。 我希望你清楚我的步骤。 –