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>
    
    评论

报告相同问题?