[Java]:无法解析没有数组/子数组的JSON
我使用的是json-simple-1.1.jar 并尝试从json中获取多个(至少3个)值,如代码部分所示 我的代码适用于具有多个数组的JSON,但不适用于简单的JSON格式[Java]:无法解析没有数组/子数组的JSON,java,arrays,json,Java,Arrays,Json,我使用的是json-simple-1.1.jar 并尝试从json中获取多个(至少3个)值,如代码部分所示 我的代码适用于具有多个数组的JSON,但不适用于简单的JSON格式 { "Addresses": { "UserName": "Rahul", "Status": "Active", "CreateDate": "2017-01-09T11:39:31.244Z", "SecretAccessKey": "ABCD-EFGH-HIJK", "
{
"Addresses": {
"UserName": "Rahul",
"Status": "Active",
"CreateDate": "2017-01-09T11:39:31.244Z",
"SecretAccessKey": "ABCD-EFGH-HIJK",
"AccessKeyId": "1234567"
}
}
以下是我尝试使用的java逻辑:
public static String[] getValuesFromJson(String filename, Object key, int exp_sizeOfArray, String[] exp_keys) throws FileNotFoundException, IOException, ParseException {
String valuesFromJson[] = new String[exp_keys.length];
/** Create a JSONParser object*/
JSONParser parser = new JSONParser();
/** Read the JSON file using parser*/
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(
filename));
System.out.println("JsonObject size: "+jsonObject.keySet().size());
for (Object object : jsonObject.keySet()) {
System.out.println(jsonObject.get(object.toString()));
}
/** Get the values in JSONArray using the key*/
JSONArray jsonArray = (JSONArray) jsonObject.get(key);
for (int i = 0; i < jsonArray.size(); i++) {
/** Add the individual set from JSONArray to a JSONObject */
JSONObject subJsonObject = (JSONObject) parser.parse(jsonArray.get(i).toString());
/** Check for expected size of array */
if(subJsonObject.size() <= exp_sizeOfArray){
int index=0;
/** Check for each key value in the sub-JSONObject keySet*/
for (Object object : subJsonObject.keySet()) {
/** Iterate until the expected key value matches with the actual value*/
for (int j = 0; j < exp_keys.length; j++) {
/** Check if the expected key matches with any of the key value*/
if(object.toString().trim().equals(exp_keys[j].toString().trim())){
System.out.println("Key: '".concat(object.toString()).concat("'\tValue: '").concat(subJsonObject.get(object)+"'"));
valuesFromJson[index] = subJsonObject.get(exp_keys[j]).toString();
index++;
break;
}
}
}
}
}
/** Return the value of expected key*/
return valuesFromJson;
}
您正试图将
JSONObject
强制转换为JSONArray
,但没有数组。而是获取所有对象键并对其进行迭代
如果您转到json.org(),您可以在那里找到:
数组是值的有序集合。数组以[(左括号)开始,以](右括号)结束。值之间用(逗号)分隔。瞧!
没有将JSONObject
转换为JSONArray
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(
filename));
/** Creating another JSONObject here */
JSONObject jsonObject2 = (JSONObject) parser.parse(jsonObject.get(key).toString());
for (Object newKey : jsonObject2.keySet()) {
System.out.println("Key: '".concat(newKey.toString()).concat("'\tValue: '").concat(jsonObject2.get(newKey)+"'"));
}
谢谢你们的帮助 这似乎是不言自明的。您试图读取一个没有数组的数组。你的问题是什么?如果我可以问的话,为什么你认为JSON代码中总是有一个数组呢。。。我以前的json格式是这样的:
{Addresses:[{“One”:“1”,“Two”:“2”}{“A”:“Apple”,“B”:“Ball”}]
,所以我考虑在我创建JSONObject时,我可以将它们作为JSONArray获取。是的,数组是值的集合,“而不是”就像hashmap一样。实际上,我正试图将JSONObject
下的值放到JSONArray
中进行迭代。甚至JSONObject.keySet()
也给出了“地址”作为结果:-(我明白你的意思,我的json中没有数组。那么我应该如何从json中获取键值对?你能帮我处理代码吗?往下一层。你的object中有object。
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(
filename));
/** Creating another JSONObject here */
JSONObject jsonObject2 = (JSONObject) parser.parse(jsonObject.get(key).toString());
for (Object newKey : jsonObject2.keySet()) {
System.out.println("Key: '".concat(newKey.toString()).concat("'\tValue: '").concat(jsonObject2.get(newKey)+"'"));
}