我有一个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吗?
答案 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();
}