我是JSON的新手,问题是??,如何foreach数据JSON(其中有对象数据和对象数组数据(使用jQuery/javascript哪些数据来自ajax响应。这里,我举了一个JSON数据的例子
{
"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])
})