我能够提取名称值(Nungam Hwy),但我无法单独获得时间值(5:30 AM)。
我的代码:
以下是我从我的档案中读到的json。
private void convertJsonToSql() {
JSONParser parser = new JSONParser();
List<Time> times = new ArrayList<>();
try {
Object obj = parser.parse(new FileReader("/home/bus/route125SaturdaySouthbound"));
JSONArray jsonArray = (JSONArray) obj;
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObj = (JSONObject) jsonArray.get(i);
String stopName = (String) jsonObj.get("Name");
times = (List) jsonObj.get("Times");
System.out.println("Stop name " + stopName);
for (int j = 0; j < times.size(); j++) {
System.out.println("Time" + times.get(j));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
[
{
"StopId":0,
"Name":"Nungam Hwy",
"Times":[
{
"Time":" 5:30AM",
"RouteNumber":"12"
},
{
"Time":" 6:16AM",
"RouteNumber":"12"
}
}
]
请有人帮助我......
答案 0 :(得分:0)
使用json-simple,以下内容应该有效:
JSONArray array = (JSONArray) config.get("Times");
Iterator<JSONObject> iterator = array.iterator();
while(iterator.hasNext()){
JSONObject element = (JSONObject) iterator.next();
System.out.println("Time " + element.get("Time"));
}