简体   繁体   中英

Can I get value from JSONObject without mention its name?

My JSONObject:

{MyJSON:[{"id":"1","name":"panji","price":"100"}]}

I do this to get value of id:

menuitemArray.getJSONObject(i).getString("id");

Can I get the value of id, name or price, without mentioning its name, may be like index or another method?

您可以在JSONObject上调用names()来获取其元素名称的JSONArray - 这将允许您对它们执行逻辑或动态检索其对应的值。

You can get all the attributes of a JSONObject like this

for(Iteraor key=jsonObject.keys();itr.hasNext();) {
   jsonObject.get(key.next());
}
JSONObject obj = new JSONObject(json);
for(Iterator<String> keys=obj.keys();keys.hasNext();) {
    obj.get(keys.next());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM