首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在多维响应中读取json数据

如何在多维响应中读取json数据
EN

Stack Overflow用户
提问于 2016-12-22 05:37:06
回答 2查看 40关注 0票数 0

如何在节点中解析以下数据--我想按键读取值,比如special_price =10.0000 & cost=20.0000

代码语言:javascript
复制
   [  
   {  
      "attributeCode":"description",
      "value":"<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.</p>\r\n<ul>\r\n<li>Dual top handles.</li>\r\n<li>Adjustable shoulder strap.</li>\r\n<li>Full-length zipper.</li>\r\n<li>L 29\" x W 13\" x H 11\".</li>\r\n</ul>"
   },
   {  
      "attributeCode":"special_price",
      "value":"10.0000"
   },
   {  
      "attributeCode":"special_from_date",
      "value":"2016-12-20 00:00:00"
   },
   {  
      "attributeCode":"cost",
      "value":"20.0000"
   },
]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-22 06:16:06

如果您想解析到对象

代码语言:javascript
复制
var responseArr = [  
   {  
      "attributeCode":"description",
      "value":"<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.</p>\r\n<ul>\r\n<li>Dual top handles.</li>\r\n<li>Adjustable shoulder strap.</li>\r\n<li>Full-length zipper.</li>\r\n<li>L 29\" x W 13\" x H 11\".</li>\r\n</ul>"
   },
   {  
      "attributeCode":"special_price",
      "value":"10.0000"
   },
   {  
      "attributeCode":"special_from_date",
      "value":"2016-12-20 00:00:00"
   },
   {  
      "attributeCode":"cost",
      "value":"20.0000"
   },
]

var responseObj = responseArr.reduce(function(obj, elm){
  obj[elm.attributeCode] = elm.value;
  return obj;
}, {})

console.log(responseObj) // Object {description: "<p>The sporty Joust Duffle Bag can't be beat - not…per.</li><li>L 29" x W 13" x H 11".</li></ul>", special_price: "10.0000", special_from_date: "2016-12-20 00:00:00", cost: "20.0000"}

然后你可以responseObj.special_priceresponseObj.cost。别忘了去parseFloat。

票数 1
EN

Stack Overflow用户

发布于 2016-12-22 06:11:14

希望它能帮到你。

代码语言:javascript
复制
function parse(data, attribute){
    for(var index in data){
       if(data[index]["attributeCode"] == attribute)
          return data[index]["value"];
    }
    return null;
}

例如:

代码语言:javascript
复制
var body=[  
  {  
     "attributeCode":"description",
     "value":"<p>The sporty Joust Duffle Bag can't be beat - not in the gym, not on the luggage carousel, not anywhere. Big enough to haul a basketball or soccer ball and some sneakers with plenty of room to spare, it's ideal for athletes with places to go.</p>\r\n<ul>\r\n<li>Dual top handles.</li>\r\n<li>Adjustable shoulder strap.</li>\r\n<li>Full-length zipper.</li>\r\n<li>L 29\" x W 13\" x H 11\".</li>\r\n</ul>"
  },
  {  
     "attributeCode":"special_price",
     "value":"10.0000"
  },
  {  
     "attributeCode":"special_from_date",
     "value":"2016-12-20 00:00:00"
  },
  {  
     "attributeCode":"cost",
     "value":"20.0000"
  },
];

console.log(parse(body, "special_price"));  //10.000
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41276411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档