如何foreach or showing data JSON查看Codeigniter 4
How to foreach or showing data JSON to view Codeigniter 4
这里我是JSON的菜鸟,问题??,如何foreach数据JSON(其中有对象数据和对象数组数据) 使用 jQuery/javascript 哪些数据来自 ajax 响应。在这里,我举了一个 JSON data
的例子
{
"peserta": {
"id_peserta": "9",
"user": "14",
"event": "1",
"kontingen": "18",
"gender": "pa",
"usia_kategori": "1",
"kategori": "1",
"video": "1615912156_5cdaea8b0938db3bdd94.mp4",
"created_at": "2021-03-16 23:29:17.000000",
"updated_at": "2021-03-16 23:29:17.000000",
"status": "0",
"slug_code": "70473488fc36724e8d0b"
},
"event": [
{
"id_event": "1",
"nama": "M Jhon",
"jenis_kelamin": "1",
"tgl_lahir": "2010-06-16",
"provinsi": "35",
"kabupaten": "3519",
"kecamatan": "351912",
"kelurahan": "3519122009",
"alamat": "Indonesia",
"no_hp": "087830052034",
"pas_foto": "1615908880_511668fc47fcfda5b5cf.jpg",
"ktp": "1615908880_45e7d016c879b4d97520.jpg",
"created_at": "2021-03-16 22:34:40.000000",
"updated_at": "2021-03-16 22:34:40.000000",
"status": "1",
"slug_code": "675dba8ef1f46805cc6a",
"id_gallery": "0"
}
]
}
你可以使用JSON.parse然后操纵它
// You can have any JSON object, just an example
const json = '{"result":20, "result2":42}';
const obj = JSON.parse(json);
const keys = Object.keys(obj);
keys.forEach(key => {
// using obj[key] you can access the value for each key
console.log(obj[key])
})
这里我是JSON的菜鸟,问题??,如何foreach数据JSON(其中有对象数据和对象数组数据) 使用 jQuery/javascript 哪些数据来自 ajax 响应。在这里,我举了一个 JSON data
的例子{
"peserta": {
"id_peserta": "9",
"user": "14",
"event": "1",
"kontingen": "18",
"gender": "pa",
"usia_kategori": "1",
"kategori": "1",
"video": "1615912156_5cdaea8b0938db3bdd94.mp4",
"created_at": "2021-03-16 23:29:17.000000",
"updated_at": "2021-03-16 23:29:17.000000",
"status": "0",
"slug_code": "70473488fc36724e8d0b"
},
"event": [
{
"id_event": "1",
"nama": "M Jhon",
"jenis_kelamin": "1",
"tgl_lahir": "2010-06-16",
"provinsi": "35",
"kabupaten": "3519",
"kecamatan": "351912",
"kelurahan": "3519122009",
"alamat": "Indonesia",
"no_hp": "087830052034",
"pas_foto": "1615908880_511668fc47fcfda5b5cf.jpg",
"ktp": "1615908880_45e7d016c879b4d97520.jpg",
"created_at": "2021-03-16 22:34:40.000000",
"updated_at": "2021-03-16 22:34:40.000000",
"status": "1",
"slug_code": "675dba8ef1f46805cc6a",
"id_gallery": "0"
}
]
}
你可以使用JSON.parse然后操纵它
// You can have any JSON object, just an example
const json = '{"result":20, "result2":42}';
const obj = JSON.parse(json);
const keys = Object.keys(obj);
keys.forEach(key => {
// using obj[key] you can access the value for each key
console.log(obj[key])
})