Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript[Ajax]中使用字典类型_Javascript_Jquery_Ajax_Asp.net Mvc_Dictionary - Fatal编程技术网

在Javascript[Ajax]中使用字典类型

在Javascript[Ajax]中使用字典类型,javascript,jquery,ajax,asp.net-mvc,dictionary,Javascript,Jquery,Ajax,Asp.net Mvc,Dictionary,我正在构建一个ASP.NET MVC应用程序,它当前有一个按钮,单击该按钮后会对控制器进行Ajax调用,如下所示: function getData() { $.ajax({ url: "/Home/GetData/", type: "POST", contentType: 'application/json', success: function (data){ //need to do stuff

我正在构建一个ASP.NET MVC应用程序,它当前有一个按钮,单击该按钮后会对控制器进行Ajax调用,如下所示:

function getData() {
    $.ajax({
        url: "/Home/GetData/",
        type: "POST",
        contentType: 'application/json',
        success: function (data){
            //need to do stuff here
        }
    });
}
然后,控制器初始化一个类,将其转换为XML,然后将其转换为以下字典(这是有原因的):

public ActionResult GetData()
{
列表peeps=GetPeeps();
字符串xml=ToXml(peeps);
Dictionary stuff=ToDictionary(xml);
返回Json(stuff);
}
我希望能够用javascript在这个数据客户端“做一些事情”

我必须使用的服务器端API返回XML数据。 我必须在客户端使用的API需要字符串数组。(因此进行了转换)

有没有办法使用我在客户端上面定义的字典?有没有人可以从这个(如果可能的话)扩展到ajax调用中添加一个小方法,将字典的内容打印到消息框中?让我从如何在javascript中使用字典开始


提前感谢

您可以在ajax调用中尝试以下操作:

function getData() {
    $.ajax({
        url: "/Home/GetData/",
        type: "POST",
        contentType: 'application/json',
        success: function (data){
            console.log(data.key1); // value for key1
            //or to list all values
            for(var key in data){
                 console.log(data[key]);
            }
        }
    });
}
控制员(出于解释目的):

public ActionResult GetData()
{
//列表peeps=GetPeeps();
//字符串xml=ToXml(peeps);
//Dictionary stuff=ToDictionary(xml);
新字典
{ 
{“key1”,新列表{“a”、“b”、“c”},
{“key2”,新列表{“d”,“e”,“f”},
};
返回Json(stuff);
}

我希望这足够清楚。让我知道你是怎么做的:)

Json()做什么的?如果在编译时不知道,我该如何打印密钥?到目前为止,for循环打印出值,但是如何将其更改为同时打印出键呢?for循环中的键变量实际上是字典中的键。这回答了你的问题吗?
function getData() {
    $.ajax({
        url: "/Home/GetData/",
        type: "POST",
        contentType: 'application/json',
        success: function (data){
            console.log(data.key1); // value for key1
            //or to list all values
            for(var key in data){
                 console.log(data[key]);
            }
        }
    });
}
public ActionResult GetData()
{
    //List<People> peeps = GetPeeps();

    //string xml = ToXml(peeps);
    //Dictionary<string,List<string>> stuff = ToDictionary(xml);
    Dictionary<string,List<string>> stuff = new Dictionary<string, List<string>>
        { 
                {"key1", new List<string> {"a", "b", "c"}},
                {"key2", new List<string> {"d", "e", "f"}},
        };

    return Json(stuff);
}