基本的JSONObject,key和value間是用:隔開的
{"door":"red"}
車子的門是紅色的
String door = new JSONObject(jsonAll).getString("door");
JSONObject裡可以包含多組的key:value
{"door":"red","body":"blue","cc":"2000"}
車子的門是紅色的,車身是藍色的,排氣量是2000cc
String door = new JSONObject(jsonAll).getString("door");
String body = new JSONObject(jsonAll).getString("body");
String cc = new JSONObject(jsonAll).getString("cc");
JSONArray是由多個JSONObject所組成
[{"door":"red","body":"blue","cc":"2000"},{"door":"black","body":"white","cc":"1600"}]
第一輛車子的門是紅色的,車身是藍色的,排氣量是2000cc
第二輛車子的門是黑色的,車身是白色的,排氣量是1600cc
String door1 = new JSONArray(jsonAll).getJSONObject(0).getString("door");
String body1 = new JSONArray(jsonAll).getJSONObject(0).getString("body");
String cc1 = new JSONArray(jsonAll).getJSONObject(0).getString("cc");
String door2 = new JSONArray(jsonAll).getJSONObject(1).getString("door");
String body2 = new JSONArray(jsonAll).getJSONObject(1).getString("body");
String cc2 = new JSONArray(jsonAll).getJSONObject(1).getString("cc");
進化型JSON一(JSONObject裡有JSONObject)
{"door":"red","body":"blue","cc":{"good":"3000","normal":"2000","cheap":"1600"}}
車子的門是紅色的,車身是藍色的,排氣量分為三個等級頂級型3000cc,一般型2000cc,經濟型1600cc
String door = new JSONObject(jsonAll).getString("door");
String body = new JSONObject(jsonAll).getString("body");
String good = new JSONObject(new JSONObject(jsonAll).getString("cc")).getString("good");
String normal = new JSONObject(new JSONObject(jsonAll).getString("cc")).getString("normal");
String cheap = new JSONObject(new JSONObject(jsonAll).getString("cc")).getString("cheap");
進化型JSON二(JSONObject裡有JSONArray)
{"door":"red","body":"blue","cc":[{"goodhigh":"3000","goodlow":"2900"},{"normalhigh":"2000","normallow":"1900"},{"cheaphigh":"1600","cheaplow":"1500"}]}
車子的門是紅色的,車身是藍色的,排氣量分為三個等級頂級型2900到3000cc,一般型1900到2000cc,經濟型1500到1600cc
String door = new JSONObject(jsonAll).getString("door");
String body = new JSONObject(jsonAll).getString("body");
String goodhigh = new JSONArray(new JSONObject(jsonAll).getString("cc")).getJSONObject(0).getString("goodhigh");
String goodlow = new JSONArray(new JSONObject(jsonAll).getString("cc")).getJSONObject(0).getString("goodlow");
String normalhigh = new JSONArray(new JSONObject(jsonAll).getString("cc")).getJSONObject(1).getString("normalhigh");
String normallow = new JSONArray(new JSONObject(jsonAll).getString("cc")).getJSONObject(1).getString("normallow");
String cheaphigh = new JSONArray(new JSONObject(jsonAll).getString("cc")).getJSONObject(2).getString("cheaphigh");
String cheaplow = new JSONArray(new JSONObject(jsonAll).getString("cc")).getJSONObject(2).getString("cheaplow");