如何在ajax中解析json字符串?

时间:2013-11-22 14:22:28

标签: ajax json cordova

我有这样的回答如何解析这些数据?任何建议

  

{ “响应”:[{ “作业ID”: “29”, “消息”: “egfn”, “lattitude”: “26.9514731”, “经度”: “89.4524783”, “job_status”: “P”, “日期”:“2013年11月21日   三点33分34" 秒, “用户名”: “用户”},{ “JOB_ID”: “28”, “消息”: “egfn”, “lattitude”: “26.9514731”, “经度”: “89.4524783”,” job_status “:” P “ ”日期“:” 2013年11月21日   三点33分34" 秒, “用户名”: “拉杰夫”},{ “JOB_ID”: “27”, “消息”: “egfn”, “lattitude”: “26.9514731”, “经度”: “89.4524783”,” job_status “:” P “ ”日期“:” 2013年11月21日   03:33:34“,”“username”:“pankaj”},{“job_id”:“26”,“message”:“测试工作为   你 “ ”lattitude“: ”25.4558641“, ”东经“: ”80.1325838“, ”job_status“: ”P“, ”日期“:” 2013年11月21日   01:09:58“,”username“:”users“},{”job_id“:”24“,”message“:”test job for   你 “ ”lattitude“: ”25.4558641“, ”东经“: ”80.1325838“, ”job_status“: ”d“, ”日期“:” 2013年11月21日   01:09:58“,”“username”:“amit”},{“job_id”:“25”,“message”:“测试工作为   你 “ ”lattitude“: ”25.4558641“, ”东经“: ”80.1325838“, ”job_status“: ”P“, ”日期“:” 2013年11月21日   1点09分58" 秒, “用户名”: “测试”}]}

提前致谢

3 个答案:

答案 0 :(得分:1)

你可以这样做:

JSON.parse('{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"users"},{"job_id":"24","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21 01:09:58","username":"amit"},{"job_id":"25","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"test"}]}')

编辑:

获取所有job_id执行此操作:

var obj = JSON.parse('{"response":[{"job_id":"29","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"users"},{"job_id":"28","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"rajeev"},{"job_id":"27","message":"egfn","lattitude":"26.9514731","longitude":"89.4524783","job_status":"P","date":"2013-11-21 03:33:34","username":"pankaj"},{"job_id":"26","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"users"},{"job_id":"24","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"D","date":"2013-11-21 01:09:58","username":"amit"},{"job_id":"25","message":"test job for you","lattitude":"25.4558641","longitude":"80.1325838","job_status":"P","date":"2013-11-21 01:09:58","username":"test"}]}')

var job_id_array = new Array();

for(var i = 0;i< obj.response.length ; i++){
    job_id_array.push(obj.response[i].job_id);
}

答案 1 :(得分:1)

您可以使用JQuery.parseJSON()

更多详情:http://api.jquery.com/jQuery.parseJSON/

答案 2 :(得分:1)

如果您的JSON字符串存储在json变量中,您可以执行以下操作(假设环境支持ECMAScript5):

var o =  JSON.parse(json);
var result=[];
o.response.forEach(function(e){
    result.push(e.job_id);
});
console.log(result);

输出result数组的内容:

["29", "28", "27", "26", "24", "25"]

这是一个解决您问题的live demogood answer