javascript - Cannot get values from JSON array object -
with json.stringify can see have data, life of me how can 1 loop , values, want lat , lng , pass them setmark() thats commented out now.
function setmarkers(map) { var data = { optstatus: $("input[name='optstatus']:checked").attr("id"), sortorder: $('#sortorder').val() }; var stemp = ""; $.ajax({ type: 'get', url: '/meterreadsdue/getmarkers', async: true, datatype: 'json', success: function (data) { var myarray = data; $("#test1").append(json.stringify(data)); //setmark(map,lat,lng); } }); }
the div output json stringified text below...
{ "contentencoding": null, "contenttype": null, "data": "[{'lat':55.86001,'lng':-4.24842,'content':'08elster-x06a245926'},{'lat':55.86001,'lng':-4.24842,'content':'06elster11w722962'},{'lat':55.86001,'lng':-4.24842,'content':'06elster13m412917'},{'lat':55.86001,'lng':-4.24842,'content':'06elster14h760382'},{'lat':55.86001,'lng':-4.24842,'content':'06elster10m097604'},{'lat':55.86001,'lng':-4.24842,'content':'06elster11m763299'},{'lat':55.86001,'lng':-4.24842,'content':'06elster13w700357'},{'lat':55.86001,'lng':-4.24842,'content':'07100043500a012550'},{'lat':55.86001,'lng':-4.24842,'content':'07100043675521477'},{'lat':55.86001,'lng':-4.24842,'content':'07100330200m018100'},{'lat':55.86001,'lng':-4.24842,'content':'07100043582490025'},{'lat':55.86001,'lng':-4.24842,'content':'06elster04m227373'},{'lat':55.86001,'lng':-4.24842,'content':'08elster-x88388817'},{'lat':55.86001,'lng':-4.24842,'content':'07100037098w006075'},{'lat':55.86001,'lng':-4.24842,'content':'06elster04m378296'},{'lat':55.86001,'lng':-4.24842,'content':'07100037187608261'},{'lat':55.86001,'lng':-4.24842,'content':'07100043587074857'},{'lat':55.86001,'lng':-4.24842,'content':'06elster83246929'},{'lat':55.86001,'lng':-4.24842,'content':'07100330205m086806'},{'lat':55.86001,'lng':-4.24842,'content':'06elster07a091225'}]", "jsonrequestbehavior": 1, "maxjsonlength": null, "recursionlimit": null }
if @ outputted json, can see data
property contains string contains json-like string. unfortunately, it’s not valid json uses single quotes instead of double quotes strings. you’re lucky particular output format not seem contain double quotes ever, can replace them first , parse it:
success: function (data) { var realdata = data.data.replace(/'/g, '"'); // replace single double quotes realdata = json.parse(realdata); // parse json $("#test1").append(json.stringify(realdata)); // realdata array of objects, iterate on realdata.foreach(function (marker) { setmark(map, marker.lat, marker.lng); }); }
Comments
Post a Comment