java org.json.JSONException: JSONObject 文本必须以 '{' at 1 [character 2 line 1] 开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31050830/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:00:17 来源:igfitidea点击:
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
提问by ricardoramos
Iam parsing a json using org.json in java . My json list like:
我在 java 中使用 org.json 解析 json 。我的 json 列表如下:
[ { "id": "f187b01b145c4171b66d4a2ecabb8f44" }, { "id": "a5e66b7462e24924a03e89f0619a2398" }, { "id": "2fb3627360db4ab78a0b3f27527b1983" } ]
I fetch data from the java code :
我从java代码中获取数据:
JSONObject json = new JSONObject(response.getEntity(String.class));
Jobs[] obj = new Gson().fromJson(json.toString(), Jobs[].class);
System.out.println(obj.toString());
But it gives an exception :
但它给出了一个例外:
Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONObject.<init>(JSONObject.java:197)
at org.json.JSONObject.<init>(JSONObject.java:324)
at br.usp.icmc.teste.ConnectionRestClient.getSaucelabsGetJobs(ConnectionRestClient.java:80)
at br.usp.icmc.teste.TestePrincipal.main(TestePrincipal.java:9)
why it did not recognized as JSONArray ? Where am I wrong ?
为什么它没有被识别为 JSONArray ?我哪里错了?
采纳答案by Srikanth A
U can try with below code. There are many ways to do it, below is one of the way
JSONArray jsonArray = new JSONArray(response.getEntity(String.class));
for(int i =0; i< jsonArray.length(); i++){
if(jsonArray.get(i) instanceof JSONObject){
JSONObject jsnObj = (JSONObject)jsonArray.get(i);
String finalValue = (String)jsnObj.get("id");
}
}