java 获取 jsonarray 键名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30412603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get jsonarray key name
提问by k.nadonenko
I need to get a key name of JsonArray, so JSON looks like this, please not, that JSON is starting with array brackets, and inside it it has objects, that was made i guess because back end will have the ability to add objects.
我需要获取 JsonArray 的键名,所以 JSON 看起来像这样,请不要,JSON 以数组括号开头,并且在其中包含对象,我猜这是因为后端将能够添加对象。
[
{
"tehnology": [ ]
},
{
"science": []
}
]
So i need to get the names from it "technology" and "science", because json can dynamically change, how can I implement it?
所以我需要从中获得“技术”和“科学”的名称,因为json可以动态变化,我该如何实现呢?
回答by Blackbelt
The JSONArraycontains JSONObjects. Retrieve every JSONObjectand use keys()to access the key defined in every JSONObject
该JSONArray包含JSONObject秒。检索每个JSONObject并用于keys()访问每个中定义的密钥JSONObject
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject object = jArray.optJSONObject(i);
Iterator<String> iterator = object.keys();
while(iterator.hasNext()) {
String currentKey = iterator.next();
}
}

