使用jquery解析JSON以获取一些值

Parsing JSON with jquery to get some value

本文关键字:获取 jquery 解析 JSON 使用      更新时间:2023-09-26

这是我的JSON

{
  "rajaongkir": {
    "query": {
      "origin": "23",
      "destination": "152",
      "weight": 1500,
      "courier": "all"
    },
    "status": {
      "code": 200,
      "description": "OK"
    },
    "origin_details": {
      "city_id": "23",
      "province_id": "9",
      "province": "Jawa Barat",
      "type": "Kota",
      "city_name": "Bandung",
      "postal_code": "40000"
    },
    "destination_details": {
      "city_id": "152",
      "province_id": "6",
      "province": "DKI Jakarta",
      "type": "Kota",
      "city_name": "Jakarta Pusat",
      "postal_code": "10000"
    },
    "results": [
      {
        "code": "pos",
        "name": "POS Indonesia (POS)",
        "costs": [
          {
            "service": "Surat Kilat Khusus",
            "description": "Surat Kilat Khusus",
            "cost": [
              {
                "value": 16500,
                "etd": "2-4",
                "note": ""
              }
            ]
          },
          {
            "service": "Express Next Day",
            "description": "Express Next Day",
            "cost": [
              {
                "value": 22000,
                "etd": "1",
                "note": ""
              }
            ]
          }
        ]
      },
      {
        "code": "jne",
        "name": "Jalur Nugraha Ekakurir (JNE)",
        "costs": [
          {
            "service": "OKE",
            "description": "Ongkos Kirim Ekonomis",
            "cost": [
              {
                "value": 18000,
                "etd": "2-3",
                "note": ""
              }
            ]
          },
          {
            "service": "REG",
            "description": "Layanan Reguler",
            "cost": [
              {
                "value": 20000,
                "etd": "1-2",
                "note": ""
              }
            ]
          },
          {
            "service": "YES",
            "description": "Yakin Esok Sampai",
            "cost": [
              {
                "value": 30000,
                "etd": "1-1",
                "note": ""
              }
            ]
          }
        ]
      },
      {
        "code": "tiki",
        "name": "Citra Van Titipan Kilat (TIKI)",
        "costs": [
          {
            "service": "SDS",
            "description": "Same Day Service",
            "cost": [
              {
                "value": 135000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "HDS",
            "description": "Holiday Delivery Service",
            "cost": [
              {
                "value": 49000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "ONS",
            "description": "Over Night Service",
            "cost": [
              {
                "value": 26000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "REG",
            "description": "Regular Service",
            "cost": [
              {
                "value": 17000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "ECO",
            "description": "Economi Service",
            "cost": [
              {
                "value": 14000,
                "etd": "",
                "note": ""
              }
            ]
          }
        ]
      }
    ]
  }
}

我的问题是如何获得"代码"(在rajangkir->result[]->code内部)?我试着使用这个jquery来获取所有的值并附加到我的selectbox中。

$.each(response['rajaongkir']['results']['code'], function(i,n){
            cou = '<option value="'+n['code']+'">'+n['code']+'</option>';
            cou = cou + '';
            $(id).append(cou);
            });

但是里面出了点问题。。我想从json(pos,jne,tiki)中给我的selectbox 3值

有人能帮我吗?

看起来您正在尝试在JSON中迭代"results"数组。

尝试删除['code'],例如:

$.each(response['rajaongkir']['results'], function(i,n){
...