我可以从JSONObject获取值而不提及它的名字吗?

时间:2022-02-13 23:40:52

My JSONObject:

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

I do this to get value of id:

我这样做是为了获得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?

我可以获取id,名称或价格的值,而不提及其名称,可能像索引或其他方法?

3 个解决方案

#1


9  

You can call names() on a JSONObject to get a JSONArray of the names of its elements - this would let you do logic on them or retrieve their corresponding values dynamically.

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

#2


1  

You can get all the attributes of a JSONObject like this

您可以像这样获取JSONObject的所有属性

for(Iteraor key=jsonObject.keys();itr.hasNext();) {
   jsonObject.get(key.next());
}

#3


1  

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

#1


9  

You can call names() on a JSONObject to get a JSONArray of the names of its elements - this would let you do logic on them or retrieve their corresponding values dynamically.

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

#2


1  

You can get all the attributes of a JSONObject like this

您可以像这样获取JSONObject的所有属性

for(Iteraor key=jsonObject.keys();itr.hasNext();) {
   jsonObject.get(key.next());
}

#3


1  

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