weixin_33727510 2017-02-04 14:53 采纳率: 0%
浏览 37

使用jQuery读取JSON

I have a JSON result after Ajax as following

 {
"results": [

{
    "id": " 14903",
  "nsid" : "10438",
    "name": "001-2501220",
  "label":"001-2501220 End Seal",
  "price":"12.6"
}, 
{
    "id": " 14904",
  "nsid" : "10439",
    "name": "001-2501231",
  "label":"001-2501231 Poppet",
  "price":"12.3"
}, 
{
    "id": " 14917",
  "nsid" : "3391",
    "name": "PW16001",
  "label":"PW16001 Caliper Piston 38.1mm Lucas TRW handbrake",
  "price":"26.5"
} 
]}

I am facing difficulties reading this. I have tried as

$.ajax({
          type: "POST",
          url: url,
          data: "partnumber=" +value,
          cache: false,
          success: function(data){
            console.log(data);
            for (var i in data.results) {
                var name = data.results[i].name;
                var text = data.results[i].label;
                console.log(name);
                console.log(text);
            }
          },
          error: function(xhr, status, error) {
              var err = eval("(" + xhr.responseText + ")");
              alert(err.Message);
          }
        });

But no luck.

  • 写回答

2条回答 默认 最新

  • weixin_33686714 2017-02-04 14:58
    关注

    Make sure your JSON response is returned as data and not by any other name. Also since javascript is async you will need to perform this action in the jquery ajax success() function. You can also use it outside but that requires some you will need to handle that differently. One more thing add dataType: "json" in your ajax code, it will make sure your returned data is valid JSON and you may need to parse it

    $.ajax({
              type: "POST",
              url: url,
              data: "partnumber=" +value,
              cache: false,
              dataType: "json",
              success: function(data){
    
                var data = JSON.parse(data);
                console.log(data);
                for (var i in data.results) {
                    var name = data.results[i].name;
                    var text = data.results[i].label;
                    console.log(name);
                    console.log(text);
                }
              },
              error: function(xhr, status, error) {
                  var err = eval("(" + xhr.responseText + ")");
                  alert(err.Message);
              }
            });
    

    var data = {
    "results": [
    
    {
        "id": " 14903",
      "nsid" : "10438",
        "name": "001-2501220",
      "label":"001-2501220 End Seal",
      "price":"12.6"
    }, 
    {
        "id": " 14904",
      "nsid" : "10439",
        "name": "001-2501231",
      "label":"001-2501231 Poppet",
      "price":"12.3"
    }, 
    {
        "id": " 14917",
      "nsid" : "3391",
        "name": "PW16001",
      "label":"PW16001 Caliper Piston 38.1mm Lucas TRW handbrake",
      "price":"26.5"
    } 
    ]}
    
    $.each(data.results, function(key, val) {
    console.log(val.name);
               console.log(val.id);
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向