将字符串转换为jsonObject,在对象的末尾添加空值

时间:2017-02-17 09:18:25

标签: android json

我有一个json_string。将该字符串转换为jsonObject时,它会在字符串末尾添加null 我的字符串是..

final String jsonStr=  "{\n" +
            "              \"prodCat_list\":[\n" +
            "                              {\n" +
            "                                  \"prods\":[\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"1\",\n" +
            "                                              \"sku\":\"wwww345\"\n" +
            "\n" +
            "                                              },\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"2\",\n" +
            "                                              \"sku\":\"coof23\"\n" +
            "\n" +
            "                                              },\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"3\",\n" +
            "                                              \"sku\":\"dde45\"\n" +
            "\n" +
            "                                              },\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"4\",\n" +
            "                                              \"sku\":\"5555\"\n" +
            "                                               }\n" +
            "                                          ]\n" +
            "                               },\n" +
            "                             {\n" +
            "                                \"prods\":[\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"1\",\n" +
            "                                              \"sku\":\"wwww345\"\n" +
            "\n" +
            "                                              },\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"2\",\n" +
            "                                              \"sku\":\"coof23\"\n" +
            "\n" +
            "                                              },\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"3\",\n" +
            "                                              \"sku\":\"dde45\"\n" +
            "\n" +
            "                                              },\n" +
            "                                              {\n" +
            "                                              \"cat_id\":\"9\",\n" +
            "                                              \"position\":\"4\",\n" +
            "                                              \"sku\":\"5555\"\n" +
            "\n" +
            "                                              }\n" +
            "                                     ]\n" +
            "                              },\n" +
            "                ]\n" +
            "  }";

因为我在名为jsonObject的{​​{1}}中有两个名为prods的{​​{1}},当我将此字符串转换为JSONArray

prodCat_list

这是将其转换为如下(最后添加null)

JSONObject

有人可以告诉我为什么这最后会添加null吗?

1 个答案:

答案 0 :(得分:0)

试试这个,

    final String jsonStr=  "{\n" +
        "              \"prodCat_list\":[\n" +
        "                              {\n" +
        "                                  \"prods\":[\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"1\",\n" +
        "                                              \"sku\":\"wwww345\"\n" +
        "\n" +
        "                                              },\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"2\",\n" +
        "                                              \"sku\":\"coof23\"\n" +
        "\n" +
        "                                              },\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"3\",\n" +
        "                                              \"sku\":\"dde45\"\n" +
        "\n" +
        "                                              },\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"4\",\n" +
        "                                              \"sku\":\"5555\"\n" +
        "                                               }\n" +
        "                                          ]\n" +
        "                               },\n" +
        "                             {\n" +
        "                                \"prods\":[\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"1\",\n" +
        "                                              \"sku\":\"wwww345\"\n" +
        "\n" +
        "                                              },\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"2\",\n" +
        "                                              \"sku\":\"coof23\"\n" +
        "\n" +
        "                                              },\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"3\",\n" +
        "                                              \"sku\":\"dde45\"\n" +
        "\n" +
        "                                              },\n" +
        "                                              {\n" +
        "                                              \"cat_id\":\"9\",\n" +
        "                                              \"position\":\"4\",\n" +
        "                                              \"sku\":\"5555\"\n" +
        "\n" +
        "                                              }\n" +
        "                                     ]\n" +
        "                              }\n" +
        "                ]\n" +
        "  }";



        JSONObject jsonObject= null;
        try {
            jsonObject = new JSONObject(jsonStr);
            Log.d("TAG","@@@ JSON :"+jsonObject.toString() );
        } catch (JSONException e) {
            e.printStackTrace();
        }