问题初始化的JSONObject初始化、问题、JSONObject

2023-09-13 01:22:45 作者:梦不完的梦

我想初始化的JSONObject与下面的字符串,从Web服务接收到的:

I'm trying to initialize a JSONObject with the following string, received from a web service:

"{
    "campaignid": "8",
    "campaignname": "Pilotarienak 2011",
    "campaignlink": "http:\\/\\/www.xxx.com\\/fr\\/cote-basque\\/agenda\\/2011-05-20\\/FMAAQU064FS016DV-pilotarienak-d-anglet?fromapp",
    "splash": "http:\\/\\/www.xxx.com\\/ads\\/customers\\/pilotarienak\\/320x480.jpg",
    "banner": "http:\\/\\/www.xxx.com\\/ads\\/customers\\/pilotarienak\\/320x160.jpg"
}"

这似乎是有效的JSON(它验证在jsonlint.com),但与初始化一个JSONObject的,当我得到:

It seems to be valid json (it validates in jsonlint.com), but when initializing a JSONObject with that I get:

org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

任何人都可以帮忙吗?

Anybody can help?

感谢

推荐答案

好像你正试图从额外引号中的字符串初始化它。您需要删除包装报价(我没有使用你的字符串,但举个例子,以使其更清晰):

Seems like you are trying to instantiate it from a String with extra quotes. You need to remove the wrapping quotes(I'm not using your string, but giving an example to make it clearer):

这是确定:

String jStr= "{\"param1\":\"hello\"}";
JSONObject jObj = new JSONOBject(jStr);

这是不是:

String jStr= "\"{\"param1\":\"hello\"}\"";
//  note this ^^             and this ^^ 
JSONObject jObj = new JSONOBject(jStr);