更好的JSON数据结构 [英] Better JSON data structure

查看:201
本文介绍了更好的JSON数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jQuery代码,它可以正常工作,并且能够在服务器中正确地反序列化它.

但是,当我尝试创建一个变量并将其作为JSON对象传递时,它不起作用. (注释的代码不起作用.这些值无法正确到达服务器).

参考: http://www.json.org/js.html

如何为JSON对象正确定义变量?

$(".searchCostPages").click(function () {


        var url = '/SearchDisplay/' + 'TransferSearchCriteria';

        //var searchCriteria = {};
        //searchCriteria.Accrual = "A";
        //searchCriteria.Brand = "B";

    //$.getJSON(url, {searchCriteria: searchCriteria
        //}, function (data) {
        //    if (data.length) {
        //        alert('Success');
        //    }

        //});

        $.getJSON(url, {
            "Accrual": "A",
            "Brand": "B"
                    }, function (data)
                    {
                        if (data.length)
                        {
                            alert('Success');
                        }

                    });



    });

工作-网络标题:

不起作用-网络标题:

更新

以下代码在这里工作.另请参考 jQuery Ajax参数格式不正确

    var searchCriteria = {};
    searchCriteria.Accrual = "A";
    searchCriteria.Brand = "B";

    $.getJSON(url, searchCriteria
    , function (data) {
        if (data.length) {
            alert('Success');
        }

    });


解决方案

您的两个示例未将相同的data参数传递给$.getJSON().

工作示例传递了此对象:

{
    Accrual: "A",
    Brand: "B"
}

无法正常工作的示例传递了此对象:

{
    searchCriteria: {
        Accrual: "A",
        Brand: "B"
    }
}

看到区别了吗?

要解决无法正常工作的示例(将{ searchCriteria: searchCriteria }传递到$.getJSON()的情况),可以将其更改为searchCriteria,从而删除了多余的对象层.

还请注意,这些是您在此处使用的JavaScript对象,而不是JSON.例如,您不必引用JSON所要求的诸如"Accrual"之类的属性名称. (引用属性名并没有什么害处,只是在JavaScript对象中是没有必要的.)知道何时使用JSON以及何时使用普通JavaScript对象是非常有用的.不一样的东西.

I have following jQuery code and it works fine and I am able to deserialize it in the server properly.

But when I tried to create a variable and pass that as a JSON object, it didn’t work. (The commented code didn’t work. The values didn’t reach the server correctly).

Reference: http://www.json.org/js.html

How can we define the variable correctly for the JSON object?

$(".searchCostPages").click(function () {


        var url = '/SearchDisplay/' + 'TransferSearchCriteria';

        //var searchCriteria = {};
        //searchCriteria.Accrual = "A";
        //searchCriteria.Brand = "B";

    //$.getJSON(url, {searchCriteria: searchCriteria
        //}, function (data) {
        //    if (data.length) {
        //        alert('Success');
        //    }

        //});

        $.getJSON(url, {
            "Accrual": "A",
            "Brand": "B"
                    }, function (data)
                    {
                        if (data.length)
                        {
                            alert('Success');
                        }

                    });



    });

Working - Network Header:

Not Working - Network Header:

UPDATE

Following code worked here. Also refer jQuery Ajax parameters are not formatted properly

    var searchCriteria = {};
    searchCriteria.Accrual = "A";
    searchCriteria.Brand = "B";

    $.getJSON(url, searchCriteria
    , function (data) {
        if (data.length) {
            alert('Success');
        }

    });


解决方案

Your two examples do not pass the same data argument to $.getJSON().

The working example passes this object:

{
    Accrual: "A",
    Brand: "B"
}

The non-working example passes this object:

{
    searchCriteria: {
        Accrual: "A",
        Brand: "B"
    }
}

See the difference?

To fix the non-working example, where you pass { searchCriteria: searchCriteria } into $.getJSON(), you can change it to searchCriteria, thus removing the extra level of object.

Also note that these are JavaScript objects you're working with here, not JSON. For example, you don't have to quote the property names like "Accrual" as required by JSON. (It doesn't hurt anything to quote the property names, it just isn't necessary in a JavaScript object.) It's useful to know when you are dealing specifically with JSON and when you are dealing with ordinary JavaScript objects, because they're not the same thing.

这篇关于更好的JSON数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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