Java JSONObject 文本必须以 '{' at 1 [character 2 line 1] 开头,并带有 '{' 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22062056/
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
A JSONObject text must begin with '{' at 1 [character 2 line 1] with '{' error
提问by Detoxic-Soul
String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";
JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject("get");
Object level = getSth.get("2");
System.out.println(level);
I referred many solutions for parsing this link, still getting the same error in question. Can any give me a simple solution for parsing it.
我参考了许多解析此链接的解决方案,但仍然遇到相同的错误。任何人都可以给我一个简单的解析解决方案。
采纳答案by Assaf Gamliel
Your problem is that String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";
is not JSON
.
What you want to do is open an HTTP
connection to "http://www.json-generator.com/j/cglqaRcMSW?indent=4" and parse the JSON response.
你的问题String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";
是不是JSON
。
您要做的是打开HTTP
与“ http://www.json-generator.com/j/cglqaRcMSW?indent=4”的连接并解析 JSON响应。
String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";
JSONObject jsonObject = new JSONObject(JSON); // <-- Problem here!
Will not open a connection to the site and retrieve the content.
不会打开与站点的连接并检索内容。
回答by Daan Luttik
Your JSON is perfectly valid. Try using these JSON classes to parse it. http://json.org/java/
您的 JSON 完全有效。尝试使用这些 JSON 类来解析它。http://json.org/java/
回答by Detoxic-Soul
Actually,,i found a simple answer,, Jst adding the object to String Builder instead of String worked ;)
实际上,我找到了一个简单的答案,Jst 将对象添加到 String Builder 而不是 String 工作;)
StringBuilder jsonString= new StringBuilder.append("http://www.json-.com/j/cglqaRcMSW?=4");
JSON json= new JSON(jsonString.toString);
回答by bekur
I had same issue. My Json response from the server was having [, and, ]:
我有同样的问题。我来自服务器的 Json 响应是 [, and, ]:
[{"DATE_HIRED":852344800000,"FRNG_SUB_ACCT":0,"MOVING_EXP":0,"CURRENCY_CODE":"CAD ","PIN":" ","EST_REMUN":0,"HM_DIST_CO":1,"SICK_PAY":0,"STAND_AMT":0,"BSI_GROUP":" ","LAST_DED_SEQ":36}]
http://jsonlint.com/says valid json. you can copy and verify it.
http://jsonlint.com/表示有效的 json。您可以复制并验证它。
I have fixed with below code as temporary solution:
我已使用以下代码作为临时解决方案进行了修复:
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String result ="";
String output = null;
while ((result = br.readLine()) != null) {
output = result.replace("[", "").replace("]", "");
JSONObject jsonObject = new JSONObject(output);
JSONArray jsonArray = new JSONArray(output);
.....
}
回答by palossyl
I had the same, there was an empty new line character at the beginning. That solved it:
我也一样,开头有一个空的换行符。那解决了它:
int i = result.indexOf("{");
result = result.substring(i);
JSONObject json = new JSONObject(result.trim());
System.out.println(json.toString(4));
回答by AGAPU SHIVA RAMA KRISHNA
I had the same issue because of the wrong order of the code statements. Maintain the below order to resolve the issue. All get methods statements first and later httpClient methods.
由于代码语句的错误顺序,我遇到了同样的问题。保持以下顺序以解决问题。所有 get 方法语句首先是 httpClient 方法。
HttpClient httpClient = new HttpClient();
get = new GetMethod(instanceUrl+usersEndPointUri);
get.setRequestHeader("Content-Type", "application/json");
get.setRequestHeader("Accept", "application/json");
httpClient.getParams().setParameter("http.protocol.single-cookie-header", true);
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.executeMethod(get);
回答by Nagaraja JB
I had similar issue due to a small mistake, when i was trying to convert a List to json. If a List is converted to json it will return JSONArray not JSONObject.
当我尝试将列表转换为 json 时,由于一个小错误,我遇到了类似的问题。如果列表被转换为 json,它将返回 JSONArray 而不是 JSONObject。
回答by Andres Navarro
in my case my arraylist trhows me that error with the JSONObject , but i fin this solution for my array of String objects
在我的情况下,我的 arraylist 向我显示了 JSONObject 的错误,但我为我的 String 对象数组找到了这个解决方案
List<String> listStrings= new ArrayList<String>();
String json = new Gson().toJson(listStrings);
return json;
Works like charm with angular Gson version 2.8.5
使用 Angular Gson 版本 2.8.5 就像魅力一样工作
回答by Dinesh
In my case the json file encoding was a problem
在我的情况下,json 文件编码是一个问题
I was generating JSON file in vb .net with following: My.Computer.FileSystem.WriteAllText("newComponent.json", json.Trim, False)
我在 vb .net 中生成 JSON 文件如下: My.Computer.FileSystem.WriteAllText("newComponent.json", json.Trim, False)
And I tried all suggestions in this post but none helped.
我尝试了这篇文章中的所有建议,但没有任何帮助。
Eventually in Notepad++ noticed that the file created was in UTF-8-BOM
最终在 Notepad++ 中注意到创建的文件是 UTF-8-BOM
Not sure how it picked up that encoding but after I switched the encoding to UTF-8, it resolved with no other changes.
不确定它是如何获取该编码的,但是在我将编码切换为 UTF-8 后,它解决了,没有其他更改。
Hope this helps someone.
希望这可以帮助某人。
回答by sonu1986
I had the same error and struggled to fix it, then answer above by Nagaraja JB helped me to fix it. In my case:
我遇到了同样的错误并努力修复它,然后 Nagaraja JB 的回答帮助我修复了它。就我而言:
Was before: JSONObject response_json = new JSONObject(response_data);
之前是: JSONObject response_json = new JSONObject(response_data);
Changed it to: JSONArray response_json = new JSONArray(response_data);
改为:JSONArray response_json = new JSONArray(response_data);
This fixed it.
这修复了它。