JSONObject文本必须以“{”错误开头 [英] A JSONObject text must begin with '{' error

查看:232
本文介绍了JSONObject文本必须以“{”错误开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这个JSON来自我们的一个REST服务:

I have this JSON coming from one of our REST service:

[
    "{\"category_name\":[\"Industry Components\"],\"categoryId\":[1]}",
    "{\"category_name\":[\"Business Components\"],\"categoryId\":[2]}",
    "{\"category_name\":[\"Utilities\"],\"categoryId\":[3]}",
    "{\"category_name\":[\"Tools\"],\"categoryId\":[4]}
]

我使用java-json.jar来解析这个JSON,这是我试图传递的简单片段以上JSON字符串:

I am using java-json.jar to parse this JSON, this is the simple snippet where I am trying to pass above JSON string:

JSONObject jsonObject = new JSONObject(jsonStr);

但我得到的是异常:

org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

首先我认为这是因为 [] JSON中的字符,我试图替换如下:

First I assumed it's because of [ and ] characters in JSON and I tried to replace as below:

String replacedStr = jsonStr.replaceAll("\\[", "").replaceAll("\\]", "")

但即便如此,我也得到同样的例外。谁能指导我知道我做错了什么?

But even then I am getting same exception. Can anyone please guide me to know what I am doing wrong?

推荐答案

我想你应该不使用JSONObject,而是使用JSONArray

I suppose that you should use not JSONObject, but JSONArray

这篇关于JSONObject文本必须以“{”错误开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆