【问题标题】:How to foreach or showing data JSON to view Codeigniter 4如何 foreach 或显示数据 JSON 以查看 Codeigniter 4
【发布时间】:2021-03-17 08:05:15
【问题描述】:

这里我是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"
    }
  ]
}

【问题讨论】:

  • 如果您已经知道要使用forEach(),我不清楚问题是什么? MDN docs 有明确的使用说明和示例。如果您有特定问题,请在问题中发布您尝试过的代码,以便我们帮助您调试它

标签: javascript jquery json ajax codeigniter


【解决方案1】:

你可以使用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])
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多