这是我的JSONObject:
{
"per_page": 3,
"total": 12,
"data": [{
"color": "#98B2D1",
"year": 2000,
"name": "cerulean",
"id": 1,
"pantone_value": "15-4020"
}, {
"color": "#C74375",
"year": 2001,
"name": "fuchsia rose",
"id": 2,
"pantone_value": "17-2031"
}
],
"page": 1,
"total_pages": 4
}
从此我应该获得所有密钥,包括per_page
,total
,data
,color
,year
,pantone_value
,{{ 1}},name
和page
如果我使用JSONObject.names()或JSONObject.keySet()。我只得到最外面的钥匙
答案 0 :(得分:1)
package com.samples;
import java.util.Iterator;
import gvjava.org.json.JSONArray;
import gvjava.org.json.JSONException;
import gvjava.org.json.JSONObject;
public class JSONObjectSample {
public static void main (String [] args) {
String jsonString = new String("{\"per_page\": 3,\"total\": 12,\"data\": [{\"color\": \"#98B2D1\",\"year\": 2000,\"name\": \"cerulean\",\"id\": 1,\"pantone_value\": \"15-4020\" }, {\"color\": \"#C74375\",\"year\": 2001,\"name\": \"fuchsia rose\",\"id\": 2,\"pantone_value\": \"17-2031\" }], \"page\": 1,\"total_pages\": 4 }\r\n");
try {
JSONObject jsonObject = new JSONObject(jsonString);
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
System.out.println(key);
if(jsonObject.get(key) instanceof JSONArray) {
JSONArray array = (JSONArray) jsonObject.get(key);
JSONObject object = (JSONObject) array.get(0);
Iterator<String> innerKeys = object.keys();
while(innerKeys.hasNext()) {
String innerKey = innerKeys.next();
System.out.println(innerKey);
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 1 :(得分:1)
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MyClass
{
public static ArrayList<String> keylist = new ArrayList<String>();
public static void main(String args[]) throws JSONException
{
myfunction(myjson);
System.out.println(keylist);
}
public static void myfunction(JSONObject x) throws JSONException
{
JSONArray keys = x.names();
for(int i=0;i<keys.length();i++)
{
String current_key = keys.get(i).toString();
if( x.get(current_key).getClass().getName().equals("org.json.JSONObject"))
{
keylist.add(current_key);
myfunction((JSONObject) x.get(current_key));
}
else if( x.get(current_key).getClass().getName().equals("org.json.JSONArray"))
{
for(int j=0;j<((JSONArray) x.get(current_key)).length();j++)
{
if(((JSONArray) x.get(current_key)).get(j).getClass().getName().equals("org.json.JSONObject"))
{
keylist.add(current_key);
myfunction((JSONObject)((JSONArray) x.get(current_key)).get(j));
}
}
}
else
{
keylist.add(current_key);
}
}
}
}
可能性:
逻辑:
答案 2 :(得分:0)
您可以使用google gson对象解析器。 (这不是很漂亮的代码,但它完成了工作)
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.HashSet;
import java.util.Set;
public class GetKeys {
public static void main(String[] args) {
String jsonString = "{\"per_page\": 3,\"total\": 12,\"data\": [{\"color\": \"#98B2D1\",\"year\": 2000,\"name\": \"cerulean\",\"id\": 1,\"pantone_value\": \"15-4020\" }, {\"color\": \"#C74375\",\"year\": 2001,\"name\": \"fuchsia rose\",\"id\": 2,\"pantone_value\": \"17-2031\" }], \"page\": 1,\"total_pages\": 4 }\r\n";
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject) parser.parse(jsonString);
Set<String> keys = new HashSet<>();
parseAllKeys(keys, object);
keys.forEach(System.out::println);
}
private static void parseAllKeys(Set<String> keys, JsonObject object) {
keys.addAll(object.keySet());
object.entrySet().stream().filter(entry -> entry.getValue().isJsonObject()).forEach(entry -> parseAllKeys(keys, (JsonObject) entry.getValue()));
object.entrySet().stream().filter(entry -> entry.getValue().isJsonArray()).forEach(entry -> entry.getValue().getAsJsonArray().forEach(subEntry -> parseAllKeys(keys, (JsonObject) subEntry)));
}
}
答案 3 :(得分:0)
下面的示例代码将使用JsonObject并将Keys转换为小写。两种方法结合使用,这样,如果请求中有任何数组,它也将在需要时转换这些键并使用递归。
public JsonArray parseArrayKeysToLower(JsonArray arrRequest) {
JsonArray arrayTemp = new JsonArray();
for(int i = 0; i < arrRequest.size(); i++) {
if (arrRequest.getValue(i) instanceof JsonObject) {
arrayTemp.add(parseObjectKeysToLower((JsonObject) arrRequest.getValue(i)));
} else if (arrRequest.getValue(i) instanceof JsonArray) {
arrayTemp.add(parseArrayKeysToLower((JsonArray) arrRequest.getValue(i)));
} else {
arrayTemp.add(arrRequest.getValue(i));
}
}
return arrayTemp;
}
public JsonObject parseObjectKeysToLower(JsonObject request) {
JsonObject requestToLower = new JsonObject();
try {
request.forEach(e -> {
String key = e.getKey();
if (e.getValue() instanceof JsonObject) {
requestToLower.put(key.toLowerCase(), parseObjectKeysToLower((JsonObject) e.getValue()));
} else if (e.getValue() instanceof JsonArray) {
requestToLower.put(key.toLowerCase(), parseArrayKeysToLower((JsonArray) e.getValue()));
} else {
requestToLower.put(key.toLowerCase(), e.getValue());
}
});
} catch (Exception e) {
e.printStackTrace();
}
return requestToLower;
}