2013-05-30 58 views
0

如何在Android中正确解析此JSON文件?Android在JSONObject中解析JSONObjects

我需要让所有的对象说塔1(这里可能有不同数量的平日),并且在那些工作日还有不同数量的时间框架。

我已经成功地使用了一个静态方法类似

JSONObject jArray = ja.getJSONObject("towers").getJSONObject("tower1") .getJSONObject("tuesday").getJSONObject("11:45-12:20");

然后我使用getString()方法值1的值。

但他们需要动态,因为有很多可能性。

{ 
"towers": { 
"tower 1": { 
    "tuesday": { 
    "07:30-11:30": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    }, 
    "11:45-12:20": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    } 
    } 
}, 
"tower 2": { 
    "wednesday": { 
    "07:15-11:35": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    }, 
    "12:45-15:10": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    }, 
    "15:30-17:05": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    } 
    }, 
    "tuesday": { 
    "07:15-11:35": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    }, 
    "12:45-15:10": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    }, 
    "14:25-17:05": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3" 
    } 
    } 
} 
}, 
"building": { 
"building 1": { 
    "monday": { 
    "07:15-12:20": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    }, 
    "tuesday": { 
    "07:15-11:35": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    }, 
    "wednesday": { 
    "07:15-11:35": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    }, 
    "friday": { 
    "07:15-11:35": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    } 
}, 
"building 2": { 
    "saturday": { 
    "08:05-11:00": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    } 
}, 
"building 3": { 
    "monday": { 
    "12:45-15:10": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    }, 
    "tuesday": { 
    "08:55-11:35": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    }, 
    "15:30-17:55": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    }, 
    "thursday": { 
    "07:15-09:40": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    }, 
    "10:00-12:25": { 
     "value1": "test value 1", 
     "value2": "test value 2", 
     "value3": "test value 3", 
     "value4": "test value 4" 
    } 
    } 
} 
} 
} 
+0

也显示相关的部分代码你试图解析当前的json字符串 –

+0

请参阅我的编辑请... – user754730

+0

看到我的答案... –

回答

1

,如果你的JSON字符串键是动态的,那么你可以分析它为:

JSONObject root = new JSONObject(yourString); 
// get towers JSONObject 
JSONObject towers = root.getJSONObject("towers"); 
// get all Towers name from towers JSONObject 

JSONArray alltowerslist=towers.names(); 

for(int i=0;i<alltowerslist.length();i++){ 
    // get sub towers from towers 
    JSONObject sub_towers = towers.getJSONObject(alltowerslist.optString(i)); 
    // get days list from sub_towers 
    JSONArray alldayslist=sub_towers.names(); 
    for(int j=0;j<alldayslist.length();j++){ 
     // get days from sub_towers 
     JSONObject days_json = sub_towers.getJSONObject(alldayslist.optString(j)); 

     // get time json JSONObject from days_json 
     JSONArray alltimeslist=days_json.names(); 
     for(int k=0;k<days_json.length();k++){ 
     // get time from sub_towers 
     JSONObject time_json = days_json.getJSONObject(alltimeslist.optString(k)); 

     // now get all value1 from time_json 
     JSONArray allvalelist=time_json.names(); 
     for(int l=0;l<time_json.length();l++){ 
      String str_value = time_json.optString(allvalelist.optString(l)); 
      } 
     } 
    } 
} 
+0

真棒!完美的答案!非常感谢 – user754730

+0

其他问题。我该如何迭代通过“未知”孩子?例如,如果我想要所有的时间(每个孩子都不同)? – user754730