简体   繁体   English

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

[英]not able to parse json array using org.json library?

I have a below JSON Array and I am trying to parse it but it is giving me an exception: 我有一个下面的JSON数组,我正在尝试解析它,但它给了我一个例外:

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

I am using org.json.JSONArray to parse the above JSON but below code throws exception: 我正在使用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);

Here is the exception I am seeing: 这是我看到的异常:

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)

What is wrong I am doing here? 我在这里做什么错?

Put esacpe charaters around voyager like below. 将esacpe角色放置在voyager周围,如下所示。

\\\"voyager\\\"

I tested it worked. 我测试了它的工作原理。

import org.json.JSONArray;

public class Test {

    public static void main(String[] args) {
        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);
    }
}

Since it has already escape characters in JOSN you need to double escape in java to retain them. 由于在JOSN中已经有转义字符,因此您需要在Java中进行两次转义以保留它们。

\\"voyager\\" \\ “旅行者\\”

This needs to be double escaped. 这需要两次转义。 The parser is seeing the \\" as the end of the quote and expecting , or } 解析器是看到了\\"作为报价的结束和期待,}

Try 尝试

\\\"voyager\\\"

In JSON syntax, you were wrong one place - "111", because names must be strings. 在JSON语法中,您错在一个地方-“ 111”,因为名称必须是字符串。 thus, @NarayanP's code would not run on android system. 因此,@ NarayanP的代码无法在android系统上运行。

Your code throws exception, this is not json's mistake. 您的代码引发异常,这不是json的错误。 problems are in assignment line; 问题在任务栏中;

String json = "...";

if you put below value into json through http response or file reading 如果您通过http响应或文件读取将以下值放入json中

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

then actually json's value will be 那么实际上json的值将是

data: 015354803811535480347"voyager";-1;12;015354800751535480069  [escaped \u0018 etc. by stackoverflow]

if the JSON string contains a semicolon then only the part of the string up until the first semicolon encountered was being returned. 如果JSON字符串包含分号,则仅返回直到遇到的第一个分号为止的字符串部分。 thus, while parsing upper json string, data item will be same as 因此,在解析上方的json字符串时,数据项将与

015354803811535480347"voyager"

Then "-1","12" are JSON syntax errors. 那么“ -1”,“ 12”是JSON语法错误。

Following is full-code without errors. 以下是没有错误的完整代码。

String json = "[{\n" +
            "    \"response\": {\n" +
            "        \"client\": \"123456\",\n" +
            "        \"varname111\": {\n" +
            "            \"data\": \"0\\u00181535480381\\u00191535480347\\u0018\\\"voyager\\\";-1;12;0\\u00181535480075\\u00191535480069\",\n" +
            "            \"time\": \"981542121421\"\n" +
            "        }\n" +
            "    }\n" +
            "}]";

JSONArray jsonArray = null;

try {
    jsonArray= new JSONArray(json);
} catch (Exception e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM