Use a JSON array with objects with javascript

后端 未结 6 1574
执笔经年
执笔经年 2021-01-30 03:29

I have a function that will get a JSON array with objects. In the function I will be able to loop through the array, access a property and use that property. Like this:

相关标签:
6条回答
  • 2021-01-30 03:41

    @Swapnil Godambe It works for me if JSON.stringfy is removed. That is:

    $(jQuery.parseJSON(dataArray)).each(function() {  
        var ID = this.id;
        var TITLE = this.Title;
    });
    
    0 讨论(0)
  • 2021-01-30 03:43

    This is your dataArray:

    [
       {
          "id":28,
          "Title":"Sweden"
       },
       {
          "id":56,
          "Title":"USA"
       },
       {
          "id":89,
          "Title":"England"
       }
    ]
    

    Then parseJson can be used:

    $(jQuery.parseJSON(JSON.stringify(dataArray))).each(function() {  
        var ID = this.id;
        var TITLE = this.Title;
    });
    
    0 讨论(0)
  • 2021-01-30 03:43

    Your question feels a little incomplete, but I think what you're looking for is a way of making your JSON accessible to your code:

    if you have the JSON string as above then you'd just need to do this

    var jsonObj = eval('[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]');
    

    then you can access these vars with something like jsonObj[0].id etc

    Let me know if that's not what you were getting at and I'll try to help.

    M

    0 讨论(0)
  • 2021-01-30 03:44

    var datas = [{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}];
    document.writeln("<table border = '1' width = 100 >");
    document.writeln("<tr><td>No Id</td><td>Title</td></tr>"); 
    for(var i=0;i<datas.length;i++){
    document.writeln("<tr><td>"+datas[i].id+"</td><td>"+datas[i].Title+"</td></tr>");
    }
    document.writeln("</table>");

    0 讨论(0)
  • 2021-01-30 03:55

    This isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:

    var arrayOfObjects = [{
      "id": 28,
      "Title": "Sweden"
    }, {
      "id": 56,
      "Title": "USA"
    }, {
      "id": 89,
      "Title": "England"
    }];
    
    for (var i = 0; i < arrayOfObjects.length; i++) {
      var object = arrayOfObjects[i];
      for (var property in object) {
        alert('item ' + i + ': ' + property + '=' + object[property]);
      }
      // If property names are known beforehand, you can also just do e.g.
      // alert(object.id + ',' + object.Title);
    }
    

    If the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval() here.

    var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
    var arrayOfObjects = eval(string);
    // ...
    

    To learn more about JSON, check MDN web docs: Working with JSON .

    0 讨论(0)
  • 2021-01-30 03:55

    By 'JSON array containing objects' I guess you mean a string containing JSON?

    If so you can use the safe var myArray = JSON.parse(myJSON) method (either native or included using JSON2), or the usafe var myArray = eval("(" + myJSON + ")"). eval should normally be avoided, but if you are certain that the content is safe, then there is no problem.

    After that you just iterate over the array as normal.

    for (var i = 0; i < myArray.length; i++) {
        alert(myArray[i].Title);
    }
    
    0 讨论(0)
提交回复
热议问题