提取json对象并填写用户标签

时间:2013-05-16 17:09:41

标签: json

[{"id":1,"name":"ABC"},{"id":2,"name":"XYZ"}]

这是控制器以json格式返回数据的方式

我想以下列格式存储它:

var json = {
"users": [
             { "id": "1", "name": "ABC" },
             { "id": "2", "name": "XYZ" },
         ]
       }

以下是我使用但不起作用的代码。

  var json = 
  {
     "users": $.getJSON("@Url.Action("SearchCMSAdmins")")
  }              

 $("#DistributorCMSAdmin").tokenInput("@Url.Action("SearchWithName")", {
    theme: "facebook",                
    preventDuplicates: true,
    prePopulate:json.users
 });

感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

$。getJson是异步的,因此您需要使用成功回调来获取响应并执行操作。试试吧。

$.getJSON("@Url.Action("SearchCMSAdmins")",function(data){
    var json= {"users":data};
    $("#DistributorCMSAdmin").tokenInput("@Url.Action("SearchWithName")", {
       theme: "facebook",                
       preventDuplicates: true,
       prePopulate:json.users
    });
});