$.grep 不与我一起过滤 JSON
尝试使用 grep,这样我就不会对其创建的控件多次调用服务器 -
$.ajax({
type: "POST",
url: "../WebMethods/MarketPersuitMethods.aspx/GetQueryInfo",
data: '{Status: "' + Name + '", search: "' + SearchBox.text() + '" }',
contentType: "application/json; charset=utf-8",
dataType: 'json',
async: false,
success: function (d) {
var preparse = JSON.stringify($.parseJSON(d.d));
var data = $.grep(preparse, function (element, index) {
return element.status.trim() == "Pending";
});
$("[id*=TextBox2]").text(preparse);
}
});
如果我测试 preparse
变量,这将返回 JSON 格式的数据。
我无法发布 JSON,因为它太多并且需要匿名。然而,作为示例,它返回类似以下内容的内容:
[{
"Project ID": "18180",
"OPRN": null,
"Proj_Type": "2049",
"CompleteDate": "2020-05-21T00:00:00",
"SQFT": 2000,
"State": "FL ",
"County": "Orange",
"status": "Pending"
},
{
"Project ID": "18180",
"OPRN": null,
"Proj_Type": "2049",
"CompleteDate": "2020-05-21T00:00:00",
"SQFT": 2000,
"State": "SC",
"County": "Orange",
"status": "Pending"
},
{
"Project ID": "18180",
"OPRN": null,
"Proj_Type": "2049",
"CompleteDate": "2020-05-21T00:00:00",
"SQFT": 2000,
"State": "GA",
"County": "Orange",
"status": "Won"
}];
但是,当尝试 $.grep
时,我得到以下带有 data
变量的内容:
[{
{
"P,r,o,j,e,c,t, ,I,D,",:, ",1,8,1,8,0,",
",O,P,R,N,",:, ,n,u,l,l,
"P,r,o,j,_,T,y,p,e,",:, ,",2,0,4,9,",
"C,o,m,p,l,e,t,e,D,a,t,e,",:, ,",2,0,2,0,-,0,5,-,2,1,T,0,0,:,0,0,:,0,0,",
",S,Q,F,T,",:, ,2,0,0,0,,
"S,t,a,t,e,",:, ,",G,A,",
"C,o,u,n,t,y,",:, ,",O,r,a,n,g,e,",
"s,t,a,t,u,s,": ",W,o,n,,
}];
Trying to use grep so that I do not do multiple calls to my server for the controls it creates -
$.ajax({
type: "POST",
url: "../WebMethods/MarketPersuitMethods.aspx/GetQueryInfo",
data: '{Status: "' + Name + '", search: "' + SearchBox.text() + '" }',
contentType: "application/json; charset=utf-8",
dataType: 'json',
async: false,
success: function (d) {
var preparse = JSON.stringify($.parseJSON(d.d));
var data = $.grep(preparse, function (element, index) {
return element.status.trim() == "Pending";
});
$("[id*=TextBox2]").text(preparse);
}
});
This is returning JSON formatted data if I test the preparse
variable.
I can't post the JSON because it's so much and would need to be anonymoized. However as a sample it returns something like the following:
[{
"Project ID": "18180",
"OPRN": null,
"Proj_Type": "2049",
"CompleteDate": "2020-05-21T00:00:00",
"SQFT": 2000,
"State": "FL ",
"County": "Orange",
"status": "Pending"
},
{
"Project ID": "18180",
"OPRN": null,
"Proj_Type": "2049",
"CompleteDate": "2020-05-21T00:00:00",
"SQFT": 2000,
"State": "SC",
"County": "Orange",
"status": "Pending"
},
{
"Project ID": "18180",
"OPRN": null,
"Proj_Type": "2049",
"CompleteDate": "2020-05-21T00:00:00",
"SQFT": 2000,
"State": "GA",
"County": "Orange",
"status": "Won"
}];
However, when trying to to $.grep
I get the following with the data
variable:
[{
{
"P,r,o,j,e,c,t, ,I,D,",:, ",1,8,1,8,0,",
",O,P,R,N,",:, ,n,u,l,l,
"P,r,o,j,_,T,y,p,e,",:, ,",2,0,4,9,",
"C,o,m,p,l,e,t,e,D,a,t,e,",:, ,",2,0,2,0,-,0,5,-,2,1,T,0,0,:,0,0,:,0,0,",
",S,Q,F,T,",:, ,2,0,0,0,,
"S,t,a,t,e,",:, ,",G,A,",
"C,o,u,n,t,y,",:, ,",O,r,a,n,g,e,",
"s,t,a,t,u,s,": ",W,o,n,,
}];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中指出的,您根本不需要做任何准备或任何回复准备。您提供的示例是 JSON,您的 AJAX 代码告诉 JS 期待 JSON 响应 (
dataType: 'json'
),因此在您的success
回调中d
已经是 JSON。只需摆脱你的准备工作,你的代码就可以工作了:As pointed out in the comments you don't need to do any preparsing or any preparation of your response at all. The sample you've provided is JSON, and your AJAX code tells JS to expect a JSON response (
dataType: 'json'
), so in yoursuccess
callbackd
is already JSON. Simply get rid of your preparsing stuff, and your code works: