$.grep 不与我一起过滤 JSON

发布于 2025-01-15 07:55:04 字数 1828 浏览 0 评论 0原文

尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-01-22 07:55:04

正如评论中指出的,您根本不需要做任何准备或任何回复准备。您提供的示例是 JSON,您的 AJAX 代码告诉 JS 期待 JSON 响应 (dataType: 'json'),因此在您的 success 回调中 d 已经是 JSON。只需摆脱你的准备工作,你的代码就可以工作了:

let d = [
    {
        "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"
    }
];

var data = $.grep(d, function (element, index) {
    return element.status.trim() == "Pending";
});

console.dir(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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 your success callback d is already JSON. Simply get rid of your preparsing stuff, and your code works:

let d = [
    {
        "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"
    }
];

var data = $.grep(d, function (element, index) {
    return element.status.trim() == "Pending";
});

console.dir(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文