转置json对象数组

时间:2017-09-26 15:03:01

标签: json ajax

我必须遍历“price_detail”json数组对象。请教我如何驯服这种类型的物体。我已经通过许多结构但不是这种类型。我找不到这种类型的数据结构搜索“通过嵌套的json对象数组迭代”的例子

我使用的代码:

     $.each(json.data['price_detail'], function (i, item) { 
      console.log('name='+ i + ' value=' +item);
      }

  output:
  name=price value=14.7,14.7,14.7
  name=type value=coupon,coupon,coupon
  name=savings value=,75%,35%
  name=pharmacy value=Walmart,Kmart,Costco

数据具有以下结构。

{"errors": [], 
            "data": {"form": "tablet", 
            "price_detail": 
                {"price": [14.7, 14.7, 14.7], 
                "type": ["coupon", "coupon", "coupon"], 
                "savings": [null, "75%", "35%"], 
                "pharmacy": ["Walmart", "Kmart", "Costco"]},                    
                "brand": ["lortab", "maxidone", "vicodin", "norco", "xodol", "hycet"], 
                "dosage": "5mg/325mg", "generic": ["hydrocodone/acetaminophen", "lorcet", "zolvit"], 
                "prices": [14.7, 14.7, 14.7], 
                "quantity": 60, 
                "display": "Lortab, Maxidone, Vicodin, Norco, Xodol, Hycet (hydrocodone / acetaminophen, lorcet, zolvit)", 
                "manufacturer": "generic"}, 
                "success": true}

我想要一个使用药房,类型,储蓄和价格的对象;如:

       {
            ["Walmart", "coupon", NULL, 14.7], 
            ["Kmart", "coupon", "75%", 14.7], 
            ["Costco","coupon", "35%", 14.7]
        }

谢谢。

编辑:我改变了标题。在浏览网页后,我发现我需要转换json结果。请原谅我缺乏术语。

我试图使用我发现的这个函数来转置数组

    function transpose(a) {
        return Object.keys(a[0]).map(function (c) {
            return a.map(function (r) {
                return r[c];
            });
        });
    }

创建测试数据,通过函数

运行
    var testData = [   [14.7, 14.7, 14.7], 
                       ["coupon", "coupon", "coupon"], 
                       [null, "75%", "35%"], 
                       ["Walmart", "Kmart", "Costco"] 
                ]
                        var result = transpose(testData);

这给了我想要的结果。

    [14.7, "coupon", null, "Walmart"],
    [14.7, "coupon", "75%", "Kmart"],
    [14.7, "coupon", "35%", "Costco"]

现在我通过它运行'price_detail'对象

    transpose(json.data['price_detail']);

我遇到了这个错误:未捕获的TypeError:无法将undefined或null转换为object

1 个答案:

答案 0 :(得分:-1)

您声明的所需输出不是有效的JSON,但这是一个产生我认为可能对您有用的格式的示例。如果它不完全符合您的要求,您现在可以看到如何修改它以适合您。



var json = {
  "errors": [],
  "data": {
    "form": "tablet",
    "price_detail": {
      "price": [14.7, 14.7, 14.7],
      "type": ["coupon", "coupon", "coupon"],
      "savings": [null, "75%", "35%"],
      "pharmacy": ["Walmart", "Kmart", "Costco"]
    },
    "brand": ["lortab", "maxidone", "vicodin", "norco", "xodol", "hycet"],
    "dosage": "5mg/325mg",
    "generic": ["hydrocodone/acetaminophen", "lorcet", "zolvit"],
    "prices": [14.7, 14.7, 14.7],
    "quantity": 60,
    "display": "Lortab, Maxidone, Vicodin, Norco, Xodol, Hycet (hydrocodone / acetaminophen, lorcet, zolvit)",
    "manufacturer": "generic"
  },
  "success": true
};

var result = [];

for (var i = 0; i < json.data.price_detail.pharmacy.length; i++) {
  result.push({
    "pharmacy": json.data.price_detail.pharmacy[i],
    "type": json.data.price_detail.type[i],
    "savings": json.data.price_detail.savings[i],
    "price": json.data.price_detail.price[i]
  });
}
console.log(JSON.stringify(result));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;