Java 手動解析Json資料小記
阿新 • • 發佈:2019-02-19
public static void main(String[] args) throws JSONException {
String json = "{status:2,result:[{company_name:'科大',company_id:1}, " +
"{company_name:'國創',company_id:5}," +
"{company_name:'新窩',company_id:7}]} ";
JSONObject object = new JSONObject(json);
int status = object .getInt("status");
System.out.println(status);
JSONArray array = object.getJSONArray("result");
for(int i=0;i<array.length();i++) {
JSONObject obj = array.getJSONObject(i);
String name = obj.getString("company_name");
int id = obj.getInt("company_id");
System.out .println(name+","+id);
}
}
小技巧:遇到實體就JSONObject,遇到陣列就JSONArray。