【问题标题】:convert json string to json array - android java将json字符串转换为json数组 - android java
【发布时间】:2014-12-18 08:52:36
【问题描述】:

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

{
"done":true,
"data":"[
{\"id\":\"5099\",\"user_id\":\"892\"},
{\"id\":\"5100\",\"user_id\":\"892\"}
....
]"}

现在在 java android 中我想解析它,但它给了我这个错误:JSON.TypeMismatch
这是我解析 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 文件中的数据数组被引号包围了?
请帮忙

【问题讨论】:

    标签: java php android json


    【解决方案1】:

    当然,data 是一个字符串(用引号括起来的字符),它不能被读取为 JSONArray。

    如果你不能让你的 php 生成一个 JSON 数组,你可以在 Android 大小上做的,是获取 data 字符串:

    String data = httpResult.getString("data");
    

    然后你可以创建一个 JSONARRAY :

    JSONArray constructor

    JSONArray dataArray = new JSONArray(data)
    

    【讨论】:

    • 你先生是生活品味,我希望我能吻你什么的:*
    【解决方案2】:
    "data":"[
    {\"id\":\"5099\",\"user_id\":\"892\"},
    {\"id\":\"5100\",\"user_id\":\"892\"}
    ....
    ]"
    

    是一个字符串

    但是

    "data":[
    {"id":"5099","user_id":"892"},
    {"id":"5100","user_id":"892"}
    ....
    ]
    

    是数组

    【讨论】:

    • @user3425760 你从哪里拿这个 json?
    【解决方案3】:

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

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

    【讨论】:

    • 是的,我删除了我的评论,但无效的 JSON 也可能来自数据字符串中的 \n
    【解决方案4】:

    使用它来将 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();
        }
    

    【讨论】:

      【解决方案5】:

      试试这个

        JsonArray array = new  JsonArray(value)
      

      value :- 应该有正确的 jsonarray 格式。否则会抛出 JsonException

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-07
        • 2020-07-01
        • 2020-05-26
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        • 2013-03-14
        相关资源
        最近更新 更多