本文介绍了访问项目的成员在JSONArray与Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始接触使用JSON和Java。我不知道如何在JSONArray内访问字符串值。例如,我的JSON是这样的:
I'm just getting started with using json with java. I'm not sure how to access string values within a JSONArray. For instance, my json looks like this:
{
"locations": {
"record": [
{
"id": 8817,
"loc": "NEW YORK CITY"
},
{
"id": 2873,
"loc": "UNITED STATES"
},
{
"id": 1501
"loc": "NEW YORK STATE"
}
]
}
}
我的code:
JSONObject req = new JSONObject(join(loadStrings(data.json),""));
JSONObject locs = req.getJSONObject("locations");
JSONArray recs = locs.getJSONArray("record");
我在这一点上获得了记录JSONArray,但我不确定,我怎么会得到ID和禄的价值观中的for循环。很抱歉,如果这个描述不是太清楚,我有点新的节目。
I have access to the "record" JSONArray at this point, but am unsure as to how I'd get the "id" and "loc" values within a for loop. Sorry if this description isn't too clear, I'm a bit new to programming.
推荐答案
您是否尝试过使用<$c$c>JSONArray.getJSONObject(int)$c$c>,和<$c$c>JSONArray.length()$c$c>创建for循环:
Have you tried using JSONArray.getJSONObject(int)
, and JSONArray.length()
to create your for-loop:
for (int i = 0; i < recs.length(); ++i) {
JSONObject rec = recs.getJSONObject(i);
int id = rec.getInt("id");
String loc = rec.getString("loc");
// ...
}
这篇关于访问项目的成员在JSONArray与Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!