如何在Jquery中使用JSON? [英] How to use JSON with Jquery?

查看:259
本文介绍了如何在Jquery中使用JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个应用程序,它会对我服务器上的PHP脚本发出POST ajax请求。该脚本将查询数据库并返回一行记录,作为数组。 (每行一个数组,包含id,title等元素)。然后我想使用 json_encode()对这个数组进行编码,并将其传递给javascript,javascript将使用它来显示记录。

I'm working at an app which would make a POST ajax request to a PHP script on my server. The script would query the database and return a row of records, as an array. (One array for each row, containing elements such as id, title, etc). I then want to use json_encode() to encode this array, and pass it back to the javascript which will use it to display the records.

1)如何将JSON编码的字符串返回给javascript?
2)javascript如何遍历行并访问其字段?

1) How can I return the JSON encoded string to the javascript? 2) How will the javascript loop through the rows and access their fields?

推荐答案

使用jQuery获取JSON,只需使用 jQuery.getJSON()即可。或者,您可以使用任何其他AJAX工具,然后只需eval()json来获取javascript对象。

To get JSON with jQuery, just use jQuery.getJSON(). Alternatively, you can use any other AJAX tool and then just eval() the json to get a javascript object.

要遍历数组,我通常使用 jQuery.each()

To loop through an array, I usually use jQuery.each():

var recordList = yourMethodToGetRecordListWithAjax();

jQuery.each(recordList, function()
{
    alert(this.Name); // For example
});

这篇关于如何在Jquery中使用JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆