Java org.json.simple.JSONArray 不能转换为 org.json.simple.JSONObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18440098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
提问by tokhi
I'm trying to parse below json
file:
我正在尝试解析以下json
文件:
{"units":[{"id":42,
"title":"Hello World",
"position":1,
"v_id":9,
"sites":[[{"id":316,
"article":42,
"clip":133904
}],
{"length":5}]
}, ..]}
This is what I have tried:
这是我尝试过的:
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");
}
}
The code is not working when I try to parse the inner json array
, so I get:
当我尝试解析内部代码时,代码不起作用json array
,所以我得到:
Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
The exception is pointing to this line:
异常指向这一行:
JSONObject site = (JSONObject)jsonSites.get(i);
Any help? tnx.
有什么帮助吗?tnx。
回答by cyberz
The first element of the sites
array is an array, as you can see indenting the JSON:
sites
数组的第一个元素是一个数组,你可以看到缩进 JSON:
{"units":[{"id":42,
...
"sites":
[
[
{
"id":316,
"article":42,
"clip":133904
}
],
{"length":5}
]
...
}
Therefore you need to treat its value accordingly; probably you could do something like:
因此你需要相应地对待它的价值;也许你可以做这样的事情:
JSONObject site = (JSONObject)(((JSONArray)jsonSites.get(i)).get(0));
回答by Onur A.
JSONObject site=jsonSites.getJSONObject(i)
should work out
JSONObject site=jsonSites.getJSONObject(i)
应该解决
回答by Danon
I've found a working code:
我找到了一个工作代码:
JSONParser parser = new JSONParser();
Object obj = parser.parse(content);
JSONArray array = new JSONArray();
array.add(obj);
回答by Krishna D
use your jsonsimpleobject direclty like below
像下面一样使用你的 jsonsimpleobject direclty
JSONObject unitsObj = parser.parse(new FileReader("file.json");
回答by Ars
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 is right but explain it reverse
@cyberz 是对的,但反过来解释
回答by arn-arn
this worked:
这有效:
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));
}
回答by LoveToCode
You can first read the whole content of file into a 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();
}
}
Now You will have the whole content into String ( data variable ).
现在您将把整个内容转换为 String (数据变量)。
JSONParser parser = new JSONParser();
org.json.simple.JSONArray jsonArray= (org.json.simple.JSONArray) parser.parse(data);
After that you can use jsonArray as you want.
之后,您可以根据需要使用 jsonArray。
回答by ritesh yadav
JSONObject baseReq
LinkedHashMap insert = (LinkedHashMap) baseReq.get("insert");
LinkedHashMap delete = (LinkedHashMap) baseReq.get("delete");
回答by prasad
If you want to re-filter the json data you can use following method. Given example is getting all document data from couchdb.
如果要重新过滤 json 数据,可以使用以下方法。给定的示例是从 couchdb 获取所有文档数据。
{
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;
}
回答by Stacie
JSONObject site = (JSONObject)jsonSites.get(i); // Exception happens here.
The return type of jsonSites.get(i)
is JSONArray
not JSONObject
.
Because sites have two '[', two means there are two arrays here.
的返回类型jsonSites.get(i)
是JSONArray
不是JSONObject
。因为站点有两个 '[',两个意味着这里有两个数组。