将json字符串转换为json数组-android java

将json字符串转换为json数组-android java,java,php,android,json,Java,Php,Android,Json,首先,我在这个网站上读到了大量类似的问题,但没有一个能解决我的问题,所以 我有一个使用php创建的json文件: { "done":true, "data":"[ {\"id\":\"5099\",\"user_id\":\"892\"}, {\"id\":\"5100\",\"user_id\":\"892\"} .... ]"} 现在在JavaAndroid中,我想解析它,但它给了我一个错误:JSON.typemissmatch 这是我解析json文件的android代码: //class

首先,我在这个网站上读到了大量类似的问题,但没有一个能解决我的问题,所以
我有一个使用php创建的json文件:

{
"done":true,
"data":"[
{\"id\":\"5099\",\"user_id\":\"892\"},
{\"id\":\"5100\",\"user_id\":\"892\"}
....
]"}
现在在JavaAndroid中,我想解析它,但它给了我一个错误:JSON.typemissmatch
这是我解析json文件的android代码:

//class from which I get the json in a JSONObject - works fine so far
JSONObject httpResult = itemHttpRequest.get("/fetch?currentList=&sort=recent&extra=");
        try {
            JSONArray httpData = httpResult.getJSONArray("data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
但它总是给我错误。
是因为我的json文件中的数据数组被引号括起来了吗?

请帮助

当然,
数据
是一个字符串(用引号括起来的字符),不能作为JSONArray读取

如果您无法让php生成JSON数组,那么在Android size上可以做的就是使用以下命令获取
数据
字符串:

String data = httpResult.getString("data");
然后,您可以使用以下命令创建JSONARRAY:

这是一根绳子

但是


是数组

您提供的JSON无效。您需要通过删除额外的引号来更正JSON。您总是可以使用一些来发现这些类型的问题,例如

{
    "done": true,
    "data": [
        {
            "id": "5099",
            "user_id": "892"
        },
        {
            "id": "5100",
            "user_id": "892"
        }
    ]
}
试试这个

  JsonArray array = new  JsonArray(value)

值:-应具有正确的jsonarray格式。否则它将抛出一个JsonException

使用它将JSON数组转换为字符串

private String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the
         * BufferedReader.readLine() method. We iterate until the
         * BufferedReader return null which means there's no more data to
         * read. Each line will appended to a StringBuilder and returned as
         * String.
         */
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

@user3425760您从哪里来获取此json?先生,您是一位生活品味者,我希望我能吻您或做些什么:*是的,我删除了我的评论,但无效的json也可能来自数据字符串中的\n
{
    "done": true,
    "data": [
        {
            "id": "5099",
            "user_id": "892"
        },
        {
            "id": "5100",
            "user_id": "892"
        }
    ]
}
  JsonArray array = new  JsonArray(value)
private String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the
         * BufferedReader.readLine() method. We iterate until the
         * BufferedReader return null which means there's no more data to
         * read. Each line will appended to a StringBuilder and returned as
         * String.
         */
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }