我正在尝试解析json
文件:
{"units":[{"id":42,
"title":"Hello World",
"position":1,
"v_id":9,
"sites":[[{"id":316,
"article":42,
"clip":133904
}],
{"length":5}]
}, ..]}
这就是我的尝试:
Object obj = null;
JSONParser parser = new JSONParser();
Object unitsObj = parser.parse(new FileReader("file.json");
JSONObject unitsJson = (JSONObject) unitsObj;
JSONArray units = (JSONArray) unitsJson.get("units");
Iterator<String> unitsIterator = units.iterator();
while(unitsIterator.hasNext()){
Object uJson = unitsIterator.next();
JSONObject uj = (JSONObject) uJson;
obj = parser.parse(uj.get("sites").toString());
JSONArray jsonSites = (JSONArray) obj;
for(int i=0;i<jsonSites.size();i++){
JSONObject site = (JSONObject)jsonSites.get(i); // Exception happens here.
System.out.println(site.get("article");
}
}
当我尝试解析内部json array
时,代码无效,所以我得到:
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
异常指向这一行:
JSONObject site = (JSONObject)jsonSites.get(i);
有任何帮助吗? TNX。
答案 0 :(得分:17)
我找到了一个有效的代码:
JSONParser parser = new JSONParser();
Object obj = parser.parse(content);
JSONArray array = new JSONArray();
array.add(obj);
答案 1 :(得分:6)
sites
数组的第一个元素是一个数组,你可以看到缩进JSON:
{"units":[{"id":42,
...
"sites":
[
[
{
"id":316,
"article":42,
"clip":133904
}
],
{"length":5}
]
...
}
因此你需要相应地对待它的价值;可能你可以这样做:
JSONObject site = (JSONObject)(((JSONArray)jsonSites.get(i)).get(0));
答案 2 :(得分:2)
这有效:
System.out.println("resultList.toString() " + resultList);
org.json.JSONObject obj = new JSONObject(resultList);
org.json.JSONArray jsonArray = obj.getJSONArray(someField);
for(int i=0;i<jsonArray.length();i++){
System.out.println("array is " + jsonArray.get(i));
}
答案 3 :(得分:1)
JSONObject site=jsonSites.getJSONObject(i)
应该可以解决
答案 4 :(得分:1)
JSONObject obj=(JSONObject)JSONValue.parse(content);
JSONArray arr=(JSONArray)obj.get("units");
System.out.println(arr.get(1)); //this will print {"id":42,...sities ..}
@cyberz是对的,但解释它反向
答案 5 :(得分:0)
直接使用你的json简单对象
JSONObject unitsObj = parser.parse(new FileReader("file.json");
答案 6 :(得分:0)
您可以先将文件的全部内容读入String。
FileInputStream fileInputStream = null;
String data="";
StringBuffer stringBuffer = new StringBuffer("");
try{
fileInputStream=new FileInputStream(filename);
int i;
while((i=fileInputStream.read())!=-1)
{
stringBuffer.append((char)i);
}
data = stringBuffer.toString();
}
catch(Exception e){
LoggerUtil.printStackTrace(e);
}
finally{
if(fileInputStream!=null){
fileInputStream.close();
}
}
现在您将整个内容都包含在String(数据变量)中。
JSONParser parser = new JSONParser();
org.json.simple.JSONArray jsonArray= (org.json.simple.JSONArray) parser.parse(data);
之后,您可以根据需要使用jsonArray。
答案 7 :(得分:0)
messages.<locale>.xlf
答案 8 :(得分:0)
如果要重新过滤json数据,可以使用以下方法。给出的示例是从沙发床获取所有文档数据。
{
Gson gson = new Gson();
String resultJson = restTemplate.getForObject(url+"_all_docs?include_docs=true", String.class);
JSONObject object = (JSONObject) new JSONParser().parse(resultJson);
JSONArray rowdata = (JSONArray) object.get("rows");
List<Object>list=new ArrayList<Object>();
for(int i=0;i<rowdata.size();i++) {
JSONObject index = (JSONObject) rowdata.get(i);
JSONObject data = (JSONObject) index.get("doc");
list.add(data);
}
// convert your list to json
String devicelist = gson.toJson(list);
return devicelist;
}
答案 9 :(得分:0)
JSONObject site = (JSONObject)jsonSites.get(i); // Exception happens here.
jsonSites.get(i)
的返回类型是JSONArray
而不是JSONObject
。
因为站点有两个'[',所以两个意味着这里有两个数组。