无法使用 org.json 库解析 json 数组?

我有一个下面的 JSON 数组,我正在尝试解析它,但它给了我一个例外:


[{

    "response": {

        "client": "123456",

        "111": {

            "data": "0\u00181535480381\u00191535480347\u0018\"voyager\";-1;12;0\u00181535480075\u00191535480069",

            "time": "981542121421"

        }

    }

}]

我org.json.JSONArray用来解析上面的 JSON 但下面的代码抛出异常:


String json =

    "[{ \"response\": { \"client\": \"123456\", \"111\": { \"data\": \"0\u00181535480381\u00191535480347\u0018\"voyager\";-1;12;0\u00181535480075\u00191535480069\", \"time\": \"981542121421\" } } }]";

 // this line throws exception

 JSONArray jsonArray = new JSONArray(json);

这是我看到的异常:


Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at character 81

    at org.json.JSONTokener.syntaxError(JSONTokener.java:410)

    at org.json.JSONObject.<init>(JSONObject.java:222)

    at org.json.JSONTokener.nextValue(JSONTokener.java:344)

    at org.json.JSONObject.<init>(JSONObject.java:205)

    at org.json.JSONTokener.nextValue(JSONTokener.java:344)

    at org.json.JSONObject.<init>(JSONObject.java:205)

    at org.json.JSONTokener.nextValue(JSONTokener.java:344)

    at org.json.JSONArray.<init>(JSONArray.java:125)

    at org.json.JSONArray.<init>(JSONArray.java:157)

我在这里做什么错了?


胡说叔叔
浏览 417回答 3
3回答

牛魔王的故事

在 voyager 周围放置 esacpe 字符,如下所示。\\\"voyager\\\"我测试它有效。import org.json.JSONArray;public class Test {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String json = "[{ \"response\": { \"client\": \"123456\", \"111\": { \"data\": \"0\u00181535480381\u00191535480347\u0018\\\"voyager\\\";-1;12;0\u00181535480075\u00191535480069\", \"time\": \"981542121421\" } } }]";&nbsp; &nbsp; &nbsp; &nbsp; // this line throws exception&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = new JSONArray(json);&nbsp; &nbsp; }}由于它已经在 JOSN 中转义了字符,因此您需要在 Java 中双重转义以保留它们。

烙印99

《航海者》这需要双重转义。解析器将\"视为引用的结尾并期望,或}尝试\\\"voyager\\\"

MMMHUHU

在 JSON 语法中,您错了一个地方 - “111”,因为名称必须是字符串。因此,@NarayanP 的代码不会在 android 系统上运行。您的代码抛出异常,这不是 json 的错误。问题出在分配线上;String json = "...";如果您通过 http 响应或文件读取将以下值放入json"data": "0\u00181535480381\u00191535480347\u0018\"voyager\";-1;12;0\u00181535480075\u00191535480069"那么实际上json的值将是data: 015354803811535480347"voyager";-1;12;015354800751535480069&nbsp; [escaped \u0018 etc. by stackoverflow]如果 JSON 字符串包含分号,则仅返回在遇到第一个分号之前的字符串部分。因此,在解析上层 json 字符串时,数据项将与015354803811535480347"voyager"那么“-1”、“12”是 JSON 语法错误。以下是没有错误的完整代码。String json = "[{\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; \"response\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"client\": \"123456\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; \"varname111\": {\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"data\": \"0\\u00181535480381\\u00191535480347\\u0018\\\"voyager\\\";-1;12;0\\u00181535480075\\u00191535480069\",\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"time\": \"981542121421\"\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; &nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp; }\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}]";JSONArray jsonArray = null;try {&nbsp; &nbsp; jsonArray= new JSONArray(json);} catch (Exception e) {&nbsp; &nbsp; e.printStackTrace();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java