我知道JSON解析,但在我當前的項目中,我得到了不同類型的JSON格式,其中所有對象的名稱都是動態的,並且它們可能會發生更改。JSON解析動態JSON對象名稱?
{
"category_id": "1",
"parent_id": "0",
"name": "Root Catalog",
"is_active": null,
"position": "0",
"level": "0",
"children": {
"0": {
"category_id": "2",
"parent_id": "1",
"name": "Default Category",
"is_active": "1",
"position": "1",
"level": "1",
"children": {
"0": {
"category_id": "32",
"parent_id": "2",
"name": "Services",
"is_active": "1",
"position": "1",
"level": "2",
"children": {
"0": {
"category_id": "20",
"parent_id": "32",
"name": "Makeup",
"is_active": "1",
"position": "1",
"level": "3",
"children": {
"0": {
"category_id": "22",
"parent_id": "20",
"name": "Bridal Makeup",
"is_active": "1",
"position": "1",
"level": "4",
"children": {
}
},
"1": {
"category_id": "23",
"parent_id": "20",
"name": "General Makeup",
"is_active": "1",
"position": "2",
"level": "4",
"children": {
}
}
}
},
"1": {
"category_id": "21",
"parent_id": "32",
"name": "Skin Services",
"is_active": "1",
"position": "2",
"level": "3",
"children": {
"0": {
"category_id": "24",
"parent_id": "21",
"name": "Bleach",
"is_active": "1",
"position": "1",
"level": "4",
"children": {
}
},
"1": {
"category_id": "25",
"parent_id": "21",
"name": "Cleanup",
"is_active": "1",
"position": "2",
"level": "4",
"children": {
}
},
"2": {
"category_id": "26",
"parent_id": "21",
"name": "General Facials",
"is_active": "1",
"position": "3",
"level": "4",
"children": {
}
},
"3": {
"category_id": "27",
"parent_id": "21",
"name": "Masks",
"is_active": "1",
"position": "4",
"level": "4",
"children": {
}
},
"4": {
"category_id": "28",
"parent_id": "21",
"name": "Special Facials",
"is_active": "1",
"position": "5",
"level": "4",
"children": {
}
},
"5": {
"category_id": "29",
"parent_id": "21",
"name": "Threading",
"is_active": "1",
"position": "6",
"level": "4",
"children": {
}
},
"6": {
"category_id": "30",
"parent_id": "21",
"name": "Waxing",
"is_active": "1",
"position": "7",
"level": "4",
"children": {
}
},
"7": {
"category_id": "38",
"parent_id": "21",
"name": "Special Services",
"is_active": "1",
"position": "8",
"level": "4",
"children": {
}
},
"8": {
"category_id": "40",
"parent_id": "21",
"name": "Lip Care",
"is_active": "1",
"position": "9",
"level": "4",
"children": {
}
},
"9": {
"category_id": "41",
"parent_id": "21",
"name": "Eye Care",
"is_active": "1",
"position": "10",
"level": "4",
"children": {
}
}
}
},
"2": {
"category_id": "3",
"parent_id": "32",
"name": "Body Services",
"is_active": "1",
"position": "3",
"level": "3",
"children": {
"0": {
"category_id": "4",
"parent_id": "3",
"name": "Body Wraps",
"is_active": "1",
"position": "1",
"level": "4",
"children": {
}
},
"1": {
"category_id": "5",
"parent_id": "3",
"name": "Reflexology",
"is_active": "1",
"position": "2",
"level": "4",
"children": {
}
},
"2": {
"category_id": "6",
"parent_id": "3",
"name": "Special Body Services",
"is_active": "1",
"position": "3",
"level": "4",
"children": {
}
}
}
},
"3": {
"category_id": "7",
"parent_id": "32",
"name": "Hair Services",
"is_active": "1",
"position": "4",
"level": "3",
"children": {
"0": {
"category_id": "8",
"parent_id": "7",
"name": "Chemical Treatments",
"is_active": "0",
"position": "1",
"level": "4",
"children": {
}
},
"1": {
"category_id": "42",
"parent_id": "7",
"name": "Hair Extensions",
"is_active": "1",
"position": "2",
"level": "4",
"children": {
}
},
"2": {
"category_id": "9",
"parent_id": "7",
"name": "Hair Color",
"is_active": "1",
"position": "3",
"level": "4",
"children": {
}
},
"3": {
"category_id": "10",
"parent_id": "7",
"name": "Haircut",
"is_active": "1",
"position": "4",
"level": "4",
"children": {
}
},
"4": {
"category_id": "11",
"parent_id": "7",
"name": "Hair Straightening",
"is_active": "1",
"position": "5",
"level": "4",
"children": {
}
},
"5": {
"category_id": "12",
"parent_id": "7",
"name": "Hair Styling",
"is_active": "1",
"position": "6",
"level": "4",
"children": {
}
},
"6": {
"category_id": "13",
"parent_id": "7",
"name": "Hair Treatments",
"is_active": "1",
"position": "7",
"level": "4",
"children": {
}
},
"7": {
"category_id": "14",
"parent_id": "7",
"name": "Other Hair Services",
"is_active": "1",
"position": "8",
"level": "4",
"children": {
}
},
"8": {
"category_id": "15",
"parent_id": "7",
"name": "Special Hair Services",
"is_active": "1",
"position": "9",
"level": "4",
"children": {
}
}
}
},
"4": {
"category_id": "16",
"parent_id": "32",
"name": "Hands And Feet",
"is_active": "1",
"position": "5",
"level": "3",
"children": {
"0": {
"category_id": "17",
"parent_id": "16",
"name": "Manicure",
"is_active": "1",
"position": "1",
"level": "4",
"children": {
}
},
"1": {
"category_id": "18",
"parent_id": "16",
"name": "Nail Art",
"is_active": "1",
"position": "2",
"level": "4",
"children": {
}
},
"2": {
"category_id": "19",
"parent_id": "16",
"name": "Pedicure",
"is_active": "1",
"position": "3",
"level": "4",
"children": {
}
}
}
}
}
},
"1": {
"category_id": "33",
"parent_id": "2",
"name": "Memberships",
"is_active": "1",
"position": "2",
"level": "2",
"children": {
}
},
"2": {
"category_id": "34",
"parent_id": "2",
"name": "Packages",
"is_active": "1",
"position": "3",
"level": "2",
"children": {
}
},
"3": {
"category_id": "35",
"parent_id": "2",
"name": "Offers",
"is_active": "1",
"position": "4",
"level": "2",
"children": {
}
},
"4": {
"category_id": "36",
"parent_id": "2",
"name": "Customer Reviews",
"is_active": "1",
"position": "5",
"level": "2",
"children": {
}
},
"5": {
"category_id": "37",
"parent_id": "2",
"name": "Gallery",
"is_active": "1",
"position": "6",
"level": "2",
"children": {
}
},
"6": {
"category_id": "31",
"parent_id": "2",
"name": "Contact us",
"is_active": "1",
"position": "7",
"level": "2",
"children": {
}
}
}
}
}
}
所以在doInBackground()
,我做如下: -
@Override
protected String doInBackground(String... params) {
ServiceHandler serviceHandler = new ServiceHandler();
String jsonStr = serviceHandler.makeServiceCall(
Constants.SIDE_DRAWER_SERVICES, ServiceHandler.GET);
try {
JSONObject jobj = new JSONObject(jsonStr);
Log.d("", "" + jsonStr.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
在字符串「jsonStr」
我得到響應完整的數據,但是當它轉換成JSONObject的像「的JSONObject jobj =新JSONObject的(jsonStr);」我越來越喜歡這個數據的丟失: -
{
"position": "0",
"children": {
"0": {
"position": "1",
"children": {
"3": {
"position": "4",
"children": {
},
"category_id": "35",
"level": "2",
"is_active": "1",
"name": "Offers",
"parent_id": "2"
},
"2": {
"position": "3",
"children": {
},
"category_id": "34",
"level": "2",
"is_active": "1",
"name": "Packages",
"parent_id": "2"
},
"1": {
"position": "2",
"children": {
},
"category_id": "33",
"level": "2",
"is_active": "1",
"name": "Memberships",
"parent_id": "2"
},
"0": {
"position": "1",
"children": {
"3": {
"position": "4",
"children": {
"3": {
"position": "4",
"children": {
},
"category_id": "10",
"level": "4",
"is_active": "1",
"name": "Haircut",
"parent_id": "7"
},
"2": {
"position": "3",
"children": {
},
"category_id": "9",
"level": "4",
"is_active": "1",
"name": "Hair Color",
"parent_id": "7"
},
"1": {
"position": "2",
"children": {
},
"category_id": "42",
"level": "4",
"is_active": "1",
"name": "Hair Extensions",
"parent_id": "7"
},
"0": {
"position": "1",
"children": {
},
"category_id": "8",
"level": "4",
"is_active": "0",
"name": "Chemical Treatments",
"parent_id": "7"
},
"7": {
"position": "8",
"children": {
},
"category_id": "14",
"level": "4",
"is_active": "1",
"name": "Other Hair Services",
"parent_id": "7"
},
"6": {
"position": "7",
"children": {
},
"category_id": "13",
"level": "4",
"is_active": "1",
"name": "Hair Treatments",
"parent_id": "7"
},
"5": {
"position": "6",
"children": {
},
"category_id": "12",
"level": "4",
"is_active": "1",
"name": "Hair Styling",
"parent_id": "7"
},
"4": {
"position": "5",
"children": {
},
"category_id": "11",
"level": "4",
"is_active": "1",
"name": "Hair Straightening",
"parent_id": "7"
},
"8": {
"position": "9",
"children": {
},
"category_id": "15",
"level": "4",
"is_active": "1",
"name": "Special Hair Services",
"parent_id": "7"
}
},
"category_id": "7",
"level": "3",
"is_active": "1",
"name": "Hair Services",
"parent_id": "32"
},
"2": {
"position": "3",
"children": {
"2": {
"position": "3",
"children": {
},
"category_id": "6",
"level": "4",
"is_active": "1",
"name": "Special Body Services",
"parent_id": "3"
},
"1": {
"position": "2",
"children": {
},
我不知道什麼是我的JSON發生。請幫助我,告訴我JSON是否無效。
我認爲你的json響應是正確的,並且在將它改爲json對象之後重新排序。由於json對象的自然是無序的。 – 2014-12-09 12:00:16
那麼我缺少的數據呢,我怎麼解析它們呢? – Sanket 2014-12-09 12:00:59
您如何知道數據丟失?如果您使用logcat打印數據,則只打印一些行。 – 2014-12-09 12:05:10