从JSON树获取AJAX [英] AJAX Get from a JSON Tree
问题描述
我只是从AJAX开始,尝试使用JQuery的$ .getJSON函数(或任何相关的函数,即$ .load(),$.ajax()等).我有一个具有如下结构的JSON文件:
I'm just starting out with AJAX, trying to use JQuery's $.getJSON function (or any related, ie $.load(), $.ajax(), etc). I have a JSON file with a structure something like this:
[
{
"id": 1,
"email": "user@domain.com",
"password": "password"
},
{
"id": 2,
"email": "one@two.com",
"password": "password"
}
]
如何为具有特定电子邮件的用户在此JSON文件上调用GET(我们将其称为users.json)?我以为这是$ .getJSON中的第二个参数,但这似乎只是返回了整个树.
How can I call a GET on this JSON file (let's call it users.json) for a user with a specific email? I thought it would be the second paramter in $.getJSON, but that seems to just return the whole tree.
谢谢!
推荐答案
简短答案-您不能直接使用AJAX进行此操作. jQuery可以提取HTML和XML文档的一部分,但不能提取JSON.
Short answer - you can't do this directly with AJAX. jQuery can extract portions of HTML and XML documents, but not JSON.
最简单的答案-检索整个对象(无论如何,您都必须使用)并使用 jsonpath 得到想要的结构
Easiest answer - retrieve the entire object (you have to, anyway) and use jsonpath to get the structure you want
更长的答案-检索整个对象,并自己遍历以获取所需的对象
Longer answer - retrieve the entire object, and traverse it yourself to get the object you want
最佳答案-将电子邮件地址作为生成json的脚本的参数,并让服务器仅返回所需的数据.
Best answer - make the email address a parameter to whatever script produces the json and have the server only return the data you want.
这篇关于从JSON树获取AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!