Android无法转换为JSONObject错误

时间:2014-05-10 16:22:31

标签: java android json

我有一个从服务器返回的JSON对象数组(如下所示),

["{\"schedule\":{\"type\":\"times\"},\"videos\":{\"abc\":\"def\"}}","{\"schedule\":{\"type\":\"tod_repeat\",\"start\":\"00:09:00\"},\"videos\":{\"mk_320059\":{\"url\":\"http://m.mtvkatsomo.fi/?progId=320059\",\"siteId\":\"mk\"}}}"]

根据JSONLint(http://jsonlint.com/)似乎是有效的json。但是在android中,当我尝试将其转换为对象时,我得到一个例外,

org.json.JSONException: Value {"schedule":{"type":"times"},"videos":{"abc":"def"}} at 0 of type java.lang.String cannot be converted to JSONObject

相关代码是,

if (resp != null) {
    try {
        JSONArray lists = new JSONArray(resp);
        Log.d(CLASS_NAME, "STRING REP:"+lists.getJSONObject(0).toString()); // <-- at this line
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

似乎我在这里遗漏了一些东西,因为我似乎无法弄清楚这里的问题是什么。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

JSONArray包含字符串作为项而不是JSONObject,因此请尝试从JSONObject获取JSONArray

     JSONArray lists = new JSONArray(resp);
     for (int i = 0; i < lists.length(); i++) {
         String str_value=  lists.optString(i);
          // get JSONObject from String
          JSONObject jsonobj=new JSONObject(str_value);
       }