รูปแบบการ get Json แบบต่างๆ

  1. แบบ jsonObject ซ้อน jsonArray
{
"data": [
{
"id": 1,
"Name": "Choc Cake",
"Image": "1.jpg",
"Category": "Meal",
"Method": "",
"Ingredients": [
{
"name": "1 Cup Ice"
},
{
"name": "1 Bag Beans"
}
]
},
{
"id": 2,
"Name": "Ice Cake",
"Image": "dfdsfdsfsdfdfdsf.jpg",
"Category": "Meal",
"Method": "",
"Ingredients": [
{
"name": "1 Cup Ice"
}
]
}
]
}

วิธีใช้

JSONObject jsonObj = new JSONObject(jsonStr);

JSONArray ja_data = jsonObj.getJSONArray("data");
int length = jsonObj.length();
for(int i=0; i<length; i++) {
JSONObject jsonObj = ja_data.getJSONObject(i);
Toast.makeText(this, jsonObj.getString("Name").toString(), Toast.LENGTH_LONG).show();

// getting inner array Ingredients
JSONArray ja = jsonObj.getJSONArray("Ingredients");
int len = ja.length();

ArrayList<String> Ingredients_names = new ArrayList<>();
for(int j=0; j<len; j++) {
JSONObject json = ja.getJSONObject(j);
Ingredients_names.add(json.getString("name").toString());
}
}

2. แบบ jsonOject ซ้อน jsonOjectซ้อน jsonArray

{
"syncresponse": {
"synckey": "2011-09-30 14:52:00",
"createdtrs": [

],
"modtrs": [

],
"deletedtrs": [
{
"companyid": "UTB17",
"username": "DA",
"date": "2011-09-26",
"reportid": "31341"
}
]
}
}

วิธี get

JSONObject object = new JSONObject(result);
String syncresponse = object.getString("syncresponse");
JSONObject object2 = new JSONObject(syncresponse); //....*
String synckey = object2.getString("synckey");
JSONArray jArray1 = object2.getJSONArray("createdtrs");
JSONArray jArray2 = object2.getJSONArray("modtrs");
JSONArray jArray3 = object2.getJSONArray("deletedtrs");
for(int i = 0; i < jArray3 .length(); i++)
{
JSONObject object3 = jArray3.getJSONObject(i);
String comp_id = object3.getString("companyid");
String username = object3.getString("username");
String date = object3.getString("date");
String report_id = object3.getString("reportid");
}

//*****************************************************************

2.1 อีกแบบ ประยุตอีกนึกนะ คล้ายๆ

{
"status": "success",
"message": "",
"data": {
"banner": [
{
"event_id": "1",
"img": "http://128.199.140.254/goudon_app/mock-api/img/EventPic1.png"
},
{
"event_id": "2",
"img": "http://128.199.140.254/goudon_app/mock-api/img/EventPic1.png"
}
],
"list": [
{
"event_id": "3",
"img": "http://128.199.140.254/goudon_app/mock-api/img/Event3.png",
"title": "งานอุดร 3",
"description": "รายละเอียด",
"date": "28-30 ต.ค. 60"
},
{
"event_id": "4",
"img": "http://128.199.140.254/goudon_app/mock-api/img/Event4.png",
"title": "งานอุดร 4",
"description": "รายละเอียด",
"date": "2 พ.ย. 60"
}
]
}
}

วิธี get

try {
obj = new JSONObject(json);
String listEventData = obj.getString("data");

Logger.Log("listEventData", listEventData);
JSONObject object2 = new JSONObject(listEventData);

JSONArray list = object2.getJSONArray("list");
JSONArray banner = object2.getJSONArray("banner");


for (int i = 0 , k= 0 ; i <= list.length() & k < banner.length() ; i++,k++)

{

JSONObject jsonObjectbanner = banner.getJSONObject(i);
String event_id_banner = jsonObjectbanner.getString("event_id");
String img_banner = jsonObjectbanner.getString("img");

Logger.Log("event_id_banner",event_id_banner);
Logger.Log("img_banner",img_banner);



JSONObject obj1 = list.getJSONObject(i);
String event_id = obj1.getString("event_id");
String img = obj1.getString("img");
String title = obj1.getString("title");
String description = obj1.getString("description");
String date = obj1.getString("date");

Logger.Log("list_event_id",event_id);
Logger.Log("list_img",img);
Logger.Log("list_title",title);
Logger.Log("list_description",description);
Logger.Log("list_date",date);


Logger.Log("listEventData video_image", img);
ListItemEvent listItemEvent = new ListItemEvent();
listItemEvent.setEventId(event_id);
listItemEvent.setImg(img);
listItemEvent.setTitle(title);
listItemEvent.setDescription(description);
listItemEvent.setDate(date);


listItemEvents.add(listItemEvent);
adapter = new EventAdapter(listItemEvents, getContext(), 0);
mRecyclerView.setAdapter(adapter);
}


Logger.Log("list",list.toString());


} catch (JSONException e) {
e.printStackTrace();
}

และเมื่อ Run ดูก็จะเห็นข้อมูลดังภาพ

cr: https://stackoverflow.com/questions/7634518/getting-jsonobject-from-jsonarray

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.