将JSON数组转换为对象或访问JSON数组上的某些节点?

时间:2011-11-27 20:00:10

标签: javascript jquery json model-view-controller

我正在使用jQuery向MVC控制器发出ajax请求,然后MVC控制器提交一个对象,并返回该对象的JSON表示,该表示将返回给ajax请求。返回的JSON如下所示:

[
  {
    "Account_LocationDevices": [],
    "Account_ChannelTransactions": [],
    "Account_Location_PaymentTypes": [],
    "ID": 56,
    "AccountID": 1,
    "Name": "b",
    "Address1": "",
    "Address2": "",
    "City": "",
    "State": "",
    "ZipCode": "",
    "Country": "",
  }
]

然后我需要能够访问此JSON数组的某些“节点”,或者将其转换回对象并通过对象访问它。任何人都有关于如何获取此JSON结果并将其转换回对象或访问数组的某些部分的建议,而不使用索引号以防更改架构?

这是执行工作的jQuery

$.ajax({
                            type: "POST",
                            url: "../Company/MicrosoftRMSStoreOperations",
                            data: { locationName: inputValue },
                            datatype: "json",
                            beforeSend: function () {

                            },
                            success: function (data) {
                                alert("Location successfully added!");
                                $('#locationDropDown').append(
                                    $('<option></option>').val().html()
                                );
                                $('#inputAddLocation').val("");
                                $('#AddLocation').animate({
                                width: 'toggle',
                                }, 100, function() {
                                //after animation
                                });
                               // window.location.href = window.location.href;
                            },
                            error: function () {
                                alert("Appears the database Gods did not agree");
                            }
                        });

2 个答案:

答案 0 :(得分:0)

首先,这不是json响应,这是一个例子:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

http://json.org/example.html

如果你使用Jquery的$ .post方法,你可以将最后一个参数指定为'json'然后你可以获得如下元素:(假设你在控制器代码中提供了json结构)

var loc = data.Account_LocationDevices;

http://api.jquery.com/jQuery.post/

答案 1 :(得分:0)

我在成功时添加了这一行:

 var json = $.parseJSON(data);
然后我可以像'json [0] .ID'或'json [0] .Name'

那样访问数组

最后看看实现的代码......

 $.ajax({
                            type: "POST",
                            url: "../Company/MicrosoftRMSStoreOperations",
                            data: { locationName: inputValue },
                            datatype: "json",
                            beforeSend: function () {

                            },
                            success: function (data) {
                            var json = $.parseJSON(data);
                                alert("Location successfully added!");
                                $('#locationDropDown').append(
                                    $('<option></option>').val(json[0].ID.toString()).html(json[0].Name)
                                );
                                $('#inputAddLocation').val("");
                                $('#AddLocation').animate({
                                width: 'toggle',
                                }, 100, function() {
                                //after animation
                                });
                               // window.location.href = window.location.href;
                            },
                            error: function () {
                                alert("Appears the database Gods did not agree");
                            }
                        });