-2

我有以下 Android java 代码:

String json = "{\"Name_1\":1,\"Name_2\":0,\"Name_3\":0}";
JSONObject object = new JSONObject(json);
String[] propertyNames = JSONObject.getNames(object);
values = new String[propertyNames.length];
for (int i = 0; i < propertyNames.length; i++) {
  values[i] = String.valueOf(object.get(propertyNames[i]));
}

但我收到以下错误:该方法getNames(JSONObject)未定义类型JSONObject。我该怎么做才能得到propertyNames

4

1 回答 1

2

为什么不看JavaDoc呢?它告诉您有关keys()方法的信息。

for (Iterator<String> it = object.keys(); it.hasNext(); ) {
    String key = it.next();
    // ...
}

编辑:获取键数组:

List<String> keyList = new ArrayList<String>();
for (Iterator<String> it = object.keys(); it.hasNext(); ) {
    String key = it.next();
    keyList.add(key);
}

String[] keyArray = keyList.toArray(new String[keyList.size()]);
于 2013-01-13T18:13:21.960 回答