【问题标题】:jQuery jSon to get sub array with multiple valuejQuery jSon 获取具有多个值的子数组
【发布时间】:2018-04-15 12:56:26
【问题描述】:

我有从 PHP Curl jSon 获取的数据。

以下是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": ""
              }
            ]
          }
        ]
      }
    ]
  }
}

现在我想获取结果数据 -> 成本 -> 服务,然后从结果 -> 成本 -> 成本 -> 值中获取成本值并将其附加到组合框上。

$.each(jsonStr['rajaongkir']['results'], function(i,n){
    cou = '<option value="'+n['costs']['description']+'">'+n['costs']['description']+'</option>';
    cou = cou + '';
    $("#service").append(cou);
});

我试图运行上面的代码并得到undefined 值。

有什么方法可以得到吗?

【问题讨论】:

  • “results”和“costs”是数组,需要通过索引来访问,即results[0].costs[0].description为第一个结果和第一个cost
  • @SamLittlefair 你能举个例子吗?
  • 使用您的语法:n[0]['costs'][0]['description'],将 0 替换为您要访问的数组索引
  • 抱歉尝试 n['costs'][0]['description']
  • 使用:n['costs'][0]['cost'][0]['value'],现在作为答案发布,谢谢:)

标签: jquery json ajax curl


【解决方案1】:

这似乎是一个动态系统,在动态系统中,使用固定索引访问它是一个非常糟糕的主意。可能我误解了你的问题,但这里有一个非常灵活的解决方案。通过 ES6 语法的快速映射函数,我得到了这个数据集:

[ 
 [ { service: 'Surat Kilat Khusus', cost: 16500 },
   { service: 'Express Next Day', cost: 22000 } 
 ],
 [ { service: 'OKE', cost: 18000 },
   { service: 'REG', cost: 20000 },
   { service: 'YES', cost: 30000 } 
 ],
 [ { service: 'SDS', cost: 135000 },
   { service: 'HDS', cost: 49000 },
   { service: 'ONS', cost: 26000 },
   { service: 'REG', cost: 17000 },
   { service: 'ECO', cost: 14000 } 
 ] 
]

这有一个二维数组指示每个项目。它是一个组合的“盒子”。长度是动态的而不是固定的,这里也可以通过前面的名称将数据集设置为对象:

[ { 'POS_Indonesia_(POS)': [ [Object], [Object] ] },
  { 'Jalur_Nugraha_Ekakurir_(JNE)': [ [Object], [Object], [Object] ] },
  { 'Citra_Van_Titipan_Kilat_(TIKI)': [ [Object], [Object], [Object], [Object], [Object] ] } ]

我还确保从键中删除空格并用下划线替换它们。这样就不需要为在 js 中再次获取键提供空间,跳过整个“字符串键”。

我的代码如下所示,但请记住,还有更有效的方法:

我首先从提供的目标解构数组,您必须使用 babel 或其他一些现代转译器进行转译才能完成这项工作。我没有检查过性能,但它看起来非常干净,并且可以高度配置使用。

let [...costs] = somedata.rajaongkir.results;

costs.map(obj => {
 let [...costs] = obj.costs;
 return {
   [obj.name.split(' ').join('_')]: costs.map(obj => ({service:obj.service, cost: obj.cost[0].value}))
  }
});

【讨论】:

    【解决方案2】:

    因为“costs”是一个数组,你想通过索引访问它:

    n['costs'][0]['description']
    

    【讨论】:

    • 最佳答案(y)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    相关资源
    最近更新 更多