【发布时间】:2016-03-29 08:30:40
【问题描述】:
大家好
我在从 JSONObject 解析 JSONArray 时遇到问题。我可能只是误会了。
创建要发送的 JSONObject:
int i = 0;
JSONArray jsonArray = new JSONArray();
String line;
while ((line = bufferedReader.readLine()) != null) {
JSONObject rule = new JSONObject().put("rule", line);
jsonArray.put(i,rule);
i++;
}
return (new JSONObject().put(jsonStrings.REQUEST_RULES_ALL_RESPONSE, jsonArray));
这会在 json 对象中发送一个 json 数组,以使事情变得更简单。这是正确的。
返回的对象格式如下:
{"REQUEST_RULES_ALL_RESPONSE":[
{"rule":"something"},
{"rule":"something"},
{"rule":"something"} ]}
我想将其解析为列表规则。 读取收到的 JSONObject:
//this returns the object as described above
JSONObject jsonObject = serverData.SendData(new JSONObject().put(jsonStrings.REQUEST_RULES_ALL, " "));
//Trying to Convert to JSONArray, the get strings are correct,
//notice the REQUEST and REQUEST RESPONSE.
//problem line below
JSONArray JSONFirewallRules = new JSONArray ((JSONArray)jsonObject.get(jsonStrings.REQUEST_RULES_ALL_RESPONSE));
错误:org.json.JSONException:不是原始数组:类 org.json.JSONArray
我不明白为什么这是个问题。我想从对象中获取 JSONArray。
【问题讨论】:
-
jsonArray.put(i,rule);到 jsonArray.put(rule);