java 为什么 JSON 解析器给出错误 org.json.JSONException:在 5 [字符 6 第 1 行]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36516505/
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
Why JSON Parser is giving, error org.json.JSONException: Expected a ':' after a key at 5 [character 6 line 1]
提问by Ramgau
I am new in json and I have to get the value from web service response. I have used org.json library for this.Below is the sample json value:
我是 json 新手,我必须从 Web 服务响应中获取值。我为此使用了 org.json 库。下面是示例 json 值:
{"tms_guid": "9LaHmoHpmTd811R",
???"recharge_status": "100",
???"message": "Transaction Successful",
???"response_time": {
??????"verifyClient": 0.0281,
??????"verifyGuid": 0.8695,
??????"verifyOperator": 0.8698,
??????"verifyMsid": 0.8698,
??????"tms_guid": 1.6971,
??????"queryErr": 7.4243,
??????"StoringRecharge": 7.4358,
??????"UpdatingBalance": 7.448
???}
}
My parsing JSON input string is :
我解析 JSON 输入字符串是:
private final static String JSON_TEST_DATA
= "{"
+ "???\"tms_guid\": \"9LaHmoHpmTd811R\", "
+ "???\"recharge_status\": \"100\", "
+ "???\"message\": \"Transaction Successful\", "
+ "???\"response_time\": { "
+ "??????\"verifyClient\": 0.0281, "
+ "??????\"verifyGuid\": 0.8695, "
+ "??????\"verifyOperator\": 0.8698,"
+ "??????\"verifyMsid\": 0.8698,"
+ "??????\"tms_guid\": 1.6971,"
+ "??????\"queryErr\": 7.4243,"
+ "??????\"StoringRecharge\": 7.4358,"
+ "??????\"UpdatingBalance\": 7.448"
+ "???}"
+ "}";
public static void main(final String[] argv) throws JSONException {
public static void main(final String[] argv) 抛出 JSONException {
System.out.println(JSON_TEST_DATA);
final JSONObject testObj = new JSONObject(JSON_TEST_DATA);
System.out.println(testObj.toString());
}
}
Exception is as follows:
异常如下:
Exception in thread "main" org.json.JSONException: Expected a ':' after a key at 5 [character 6 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:432)
at org.json.JSONObject.<init>(JSONObject.java:206)
at org.json.JSONObject.<init>(JSONObject.java:310)
at com.kalsym.wsp.sp.icebeep.TestIceBeep.main(TestIceBeep.java:73)
I have seen similar post. But could not figure about the solution.
我看过类似的帖子。但无法弄清楚解决方案。
回答by Laxman G
I am not seeing any problem. I have used same your code, but i am able to execute your program :
我没有看到任何问题。我使用了与您相同的代码,但我能够执行您的程序:
You can see my code, I have useed same java api too.
你可以看到我的代码,我也使用了相同的 java api。
import org.json.JSONException;
import org.json.JSONObject;
public class TestCode {
private final static String JSON_TEST_DATA
= "{"
+ " \"tms_guid\": \"9LaHmoHpmTd811R\", "
+ " \"recharge_status\": \"100\", "
+ " \"message\": \"Transaction Successful\", "
+ " \"response_time\": { "
+ " \"verifyClient\": 0.0281, "
+ " \"verifyGuid\": 0.8695, "
+ " \"verifyOperator\": 0.8698,"
+ " \"verifyMsid\": 0.8698,"
+ " \"tms_guid\": 1.6971,"
+ " \"queryErr\": 7.4243,"
+ " \"StoringRecharge\": 7.4358,"
+ " \"UpdatingBalance\": 7.448"
+ " }"
+ "}";
public static void main (String arg[]) throws JSONException{
//System.out.println(JSON_TEST_DATA);
final JSONObject testObj = new JSONObject(JSON_TEST_DATA);
System.out.println(" --"+testObj.getString("recharge_status")+"\n");
System.out.println(testObj.toString());
}
}
may be some char-set problem.
可能是一些字符集问题。
回答by gregko
I just had a similar problem, turned out that the JSON string copied from another place contained non-breaking space characters, hard to notice even under a debugger :). I was removing HTML tags from that string (received via email) with:
我刚刚遇到了类似的问题,结果是从另一个地方复制的 JSON 字符串包含不间断的空格字符,即使在调试器下也很难注意到:)。我正在从该字符串(通过电子邮件接收)中删除 HTML 标签:
jstr = jstr.replaceAll("<.*?>", ""); // removes anything between < and >
and the result looked like valid JSON, but with these non-breaking spaces... This helped:
结果看起来像有效的 JSON,但是有这些不间断的空格......这有帮助:
jstr = jstr.replaceAll("<.*?>|\u00a0", "");