2

我有一个 jQuery http 请求,它以 JSON 字符串的形式返回一个对象。这里我要做的是将JSON转换为jQuery对象并将值分配给html元素......以下是要转换的对象的结构:

var Userrights = {

            Id: // an id element
            ScreenRights: // this is a list with following elements
            {
                ScreenName: 
                Create:
                Read: 
                Update: 
                Delete:
                Approve: 
                Access: 
                Print: 
                Email: 
            };
        };

现在,当我收到如下 JSON 字符串时:

{
    "Id": "Manager",
    "ScreenRights": [{
        "ScreenName": "CustomerScreen",
        "Create": true,
        "Read": false,
        "Update": true,
        "Delete": false,
        "Approve": true,
        "Access": true,
        "Print": true,
        "Email": true
    },
    {
        "ScreenName": "TraderScreen",
        "Create": true,
        "Read": false,
        "Update": true,
        "Delete": false,
        "Approve": true,
        "Access": true,
        "Print": true,
        "Email": true
    },
    {
        "ScreenName": "DistributorScreen",
        "Create": true,
        "Read": false,
        "Update": true,
        "Delete": false,
        "Approve": true,
        "Access": true,
        "Print": true,
        "Email": true
    }]
}​

现在我将如何将此字符串转换为上述对象结构?

4

2 回答 2

0

查询:

var obj = $.parseJSON(string);
console.log(obj);

话虽如此,如果您使用的是 jQuery 的 .ajax,您可以设置dataType:'json'

于 2012-11-28T13:26:25.697 回答
0

我认为您可以将 json 推送到某个数组中,然后您可以使循环按名称访问对象。

result[i].Create;
result[i].Read;
于 2020-08-19T21:57:06.997 回答