Use a JSON array with objects with javascript

后端 未结 6 1579
执笔经年
执笔经年 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: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);
    }
    

提交回复
热议问题