开发者

can't loop out data from json array?

开发者 https://www.devze.com 2023-03-20 14:00 出处:网络
I can\'t loop data out from response arr开发者_开发百科ay. $.post(myurl , function(response){ for (var i = 0; i < response.result.length; i++) {

I can't loop data out from response arr开发者_开发百科ay.

$.post(myurl , function(response){
      for (var i = 0; i < response.result.length; i++) {
         alert(response.result[i]);
      }
},"json");

result of returned json array is :

{"result":["data one","data two","data three"]}


You can iterate over an object's properties with a for each loop:

for (var prop in response) {
  alert(prop + ": " + response[prop]);
}


  1. Did u try using JSON.parse to obtain your data in the JSON object format?
  2. Always use jslint to see if your JSON is proper, before using JSON.parse.

EDIT : In your head tag add json2.js. Go here and download it. Then whenever u have a json string like the one in your question. Just use JSON.parse(somejsonstring);

var t = '{"result":["data one","data two","data three"]}' ; //Response Text
var p = JSON.parse(t); //p.result[0] will give u "data one"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号