简体   繁体   English

尝试解析 JSON 字符串时出错。 org.json.JSONException:JSONArray 文本必须以 '[' 开头

[英]Getting error when trying parse JSON string. org.json.JSONException: A JSONArray text must start with '['

I am trying to parse a JSON string so I could turn it into an array and iterate through each element through indexing.我正在尝试解析 JSON 字符串,以便可以将其转换为数组并通过索引遍历每个元素。 Below is my code:下面是我的代码:

String body = "{\"user\":\"d26d0op3-7da5-6ad8\",\"pass\":\"12784949-2b8c-827d\"}";

ArrayList<String> stringArray = new ArrayList<String>();

JSONArray jsonArray = new JSONArray(body);

for (int i = 0; i < jsonArray.length(); i++) {
    stringArray.add(jsonArray.getString(i));
}

System.out.println(stringArray);

When I run this code, I get the following error:当我运行此代码时,我收到以下错误:

Exception in thread "main" org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]

I tried formatting my body differently with:我尝试用不同的方式格式化我的身体:

String body = "[{\"user\":\"d26d0op3-7da5-6ad8\",\"instanceId\":\"12784949-2b8c-827d\"}]";

But then I got the following error:但后来我收到以下错误:

Exception in thread "main" org.json.JSONException: JSONArray[0] is not a String.

How would I go about correctly parsing my JSON?我将如何正确解析我的 JSON?

You might want to read up a bit on the JSON format.您可能想了解一下 JSON 格式。 Try http://json.org .试试http://json.org

In your first case, body is a JSON 'object', not an array.在您的第一种情况下, body是 JSON '对象',而不是数组。 This is similar to a dictionary, or key:value pair map.这类似于字典或键:值对 map。 A JSON object is delimited with { and }. JSON object 用 { 和 } 分隔。

To parse this body, you would use:要解析此主体,您将使用:

JSONObject job = new JSONObject(body);
String username = job.getString("user");

I think that is probably what you are after.我想这可能就是你所追求的。

In the second case, your body is a JSON array that contains one JSON Object.在第二种情况下,您的主体是一个 JSON 数组,其中包含一个 JSON Object。 A JSON array is delimited by [ and ] JSON 数组由 [ 和 ] 分隔

If you want a JSON array of strings, it would look something like this, instead:如果你想要一个 JSON 字符串数组,它看起来像这样,而不是:

 body = "[ \"a\", \"b\", \"c\" ]";

An array of integers would look like:整数数组如下所示:

 body = "[ 1,2,3,4 ]";

You can use the ObjectMapper.readValue(String, Class<T>) method to parse a value from a json string to some object.您可以使用ObjectMapper.readValue(String, Class<T>)方法将值从 json 字符串解析为某个 object。 In this case it can be a Map<String, String> :在这种情况下,它可以是Map<String, String>

String body = "{\"user\":\"d26d0op3-6ad8\",\"pass\":\"12784949-827d\"}";

Map<String, String> map = new ObjectMapper().readValue(body, Map.class);

System.out.println(map); // {user=d26d0op3-6ad8, pass=12784949-827d}

If you are expecting a list of some objects:如果您期待一些对象的列表:

String body = "[{\"user\":\"d26d0op3-6ad8\",\"pass\":\"12784949-827d\"}]";

List<Object> list = new ObjectMapper().readValue(body, List.class);

System.out.println(list); // [{user=d26d0op3-6ad8, pass=12784949-827d}]

The String body = "{"user":"d26d0op3-6ad8","pass":"12784949-827d"}" is not a array enter image description here String body = "{"user":"d26d0op3-6ad8","pass":"12784949-827d"}" 不是数组在此处输入图像描述

you can convert to json array like this String body = "[{"user":"d26d0op3-7da5-6ad8","pass":"12784949-2b8c-827d"}]";您可以像这样转换为 json 数组 String body = "[{"user":"d26d0op3-7da5-6ad8","pass":"12784949-2b8c-827d"}]";

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

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