I have json message in this format
{"id":21,"image":"binary64image","pdate":"2014-01-27"},
{"id":22,"image":"binary64image","pdate":"2014-01-27"},
{"id":21,"image":"binary64image","pdate":"2014-01-27"}
and I want to convert this into javascript array, I have tried
var txt = '{ "potholes" : [' + data + ']}';
var jsonObj = eval("(" + txt + ")");
alert(jsonObj.potholes[1].id);
and
var potholes = JSON.parse(data);
alert(potholes[1].id);
neither worked. The data is there as it is in a success to an ajax call which returns json object.
Your example work great!
simple data should be a string
data = '{id: ....'
You can convert your JSON response in JavaScript array like below :
var data = '{"id":21,"image":"binary64image","pdate":"2014-01-27"},{"id":22,"image":"binary64image","pdate":"2014-01-27"},{"id":21,"image":"binary64image","pdate":"2014-01-27"}';
var result = eval("["+data+"]"); //convert your response into JavaScript array
for(var i in result) // read your array value
alert("id : "+result[i].id+" --> image : "+result[i].image+" --> pdate : "+result[i].pdate);
You should convert your input data to an array ( fiddle ):
var input = '{"id":21,"image":"binary64image","pdate":"2014-01-27"},{"id":22,"image":"binary64image","pdate":"2014-01-27"},{"id":21,"image":"binary64image","pdate":"2014-01-27"}',
inputArr = '[' + input + ']',
arr = JSON.parse(inputArr);
for (var i in arr)
console.log(arr[i].id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.