抱歉我的英文。我不知道我是否正确解析。我有json字符串
{
"TypeCar": {
"1": {
"type": "1",
"id": "1"
},
"2": {
"type": "1",
"id": "2"
}
.
.
.
"55": {
"type": "1",
"id": "55"
}
}
好的,它很简单我得到了json字符串,在循环中我会做这样的事情
JSONObject typeCar = jsonObject.getJSONObject("TypeCar");
for(int j = 1; j < typeCar.length(); j++) {
JSONObject numberTcar = typeCar.getJSONObject(String.valueOf(j));
String type = numberTcar.getString("type");
String id = numberTcar.getString("id");
}
但如果我有像这样的json
{
"TypeCar": {
"10": {
"type": "1",
"id": "10"
},
"4": {
"type": "1",
"id": "4"
}
"14": {
"type": "1",
"id": "14"
}
}
然后我解析它?
UPD:
解析迭代器:
//my all json string
JSONObject jsonObject = new JSONObject(json.toString());
//json only TypeCar
JSONObject sensorTypes = jsonObject.getJSONObject("TypeCar");
Iterator<String> iterator = sensorTypes.keys();
while(iterator.hasNext()) {
String currentKey = iterator.next();
JSONObject obj = jsonObject.optJSONObject(currentKey);
if(obj != null) {
//its now work, what i do wrong?
String type = obj.getString("type");
String id = obj.getString("id");
}
}