2017-02-17 82 views
-2

我有一個json_string。而該字符串轉換成jsonObject它在串 我的字符串是的末尾添加空..將字符串轉換爲jsonObject在對象末尾添加空值

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" + 
      " }"; 

,因爲我在名爲prodCat_listJSONArray有兩個jsonObject與名prods,當我這個字符串轉換爲JSONObject

JSONObject jsonObject=new JSONObject(jsonStr); 

這被如下轉換它(在最後添加空)

{"prodCat_list":[{"prods": [{"sku":"wwww345","position":"1","cat_id":"9"},{"sku":"coof23","position":"2","cat_id":"9"},{"sku":"dde45","position":"3","cat_id":"9"},{"sku":"5555","position":"4","cat_id":"9"}]},{"prods":[{"sku":"wwww345","position":"1","cat_id":"9"},{"sku":"coof23","position":"2","cat_id":"9"},{"sku":"dde45","position":"3","cat_id":"9"},{"sku":"5555","position":"4","cat_id":"9"}]},null]} 

有人可以告訴我爲什麼這是最後增加null?

+0

我覺得有一個逗號超過。爲什麼不使用json_encode()技術?在平方parentesys之前的行 –

+0

爲什麼不使用json_encode()技術? –

+0

在第三個最後一行刪除逗號,\ n「+作爲它的最後一個元素 –

回答

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(); 
     } 
+0

謝謝@ user2025187在方形parentesys之前有一個逗號超過了行 –