1. 代碼
public void JsonArray(){
String jsonStr = "[{\"en_maker_name\": \"龍洞\",\"ex_maker_name\": \"沙太\",\"tollRoads\": [{\"need_fee\": \"1.46\",\"road_code\": \"30\",\"road_name\": \"華南快速\",\"toll_distance\": \"2300\"},{\"need_fee\": \"2.53\",\"road_code\": \"31\",\"road_name\": \"華南三期\",\"toll_distance\": \"4000\"}],\"toll_distance\": \"6300\",\"toll_over_weight\": \"0.00 \",\"tolls\": \"4.00\"}]";
//先轉換外層數組
JSONArray jsonarray = JSONArray.fromObject(jsonStr);
List<RouteInfoTollsStep> tollsSteps = JSONArray.toList(jsonarray, RouteInfoTollsStep.class);
for(int i=0;i<tollsSteps.size();i++){
//遍歷外層數組,拿到當前的數組對象
JSONObject jsonObject=jsonarray.getJSONObject(i);
//取出當前數組對象的tollRoads屬性數組
String jsonStr2 = jsonObject.getString("tollRoads");
JSONArray jsonarray2 = JSONArray.fromObject(jsonStr2);
List<RouteInfoTollsRoad> tollRoads = JSONArray.toList(jsonarray2, RouteInfoTollsRoad.class);
//設置第二層數組的值
tollsSteps.get(i).setTollRoads(tollRoads);
}
System.out.println("aaa================="+JSON.toJSONString(tollsSteps));
}