简体   繁体   English

我如何反序列化json字符串列表 <Dictionary<string,int> &gt;使用JQuery?

[英]How can I deserialize a json string List<Dictionary<string,int>> using JQuery?

Many thanks in advance for your help. 在此先感谢您的帮助。

I have the following code in an action which returns a json serialized string of List<Dictionary<string,int>> , but I am struggling to get the data into a usable format in the view, using JQuery. 我在执行以下操作的代码中返回了List<Dictionary<string,int>>的json序列化字符串,但是我正在努力使用JQuery将数据转换为视图中的可用格式。 I want to be able to iterate through each dictionary. 我希望能够遍历每本字典。

I have done this before using a List, which worked, so I could I suppose create a new model but I am sure I should be able to deserialize this. 我在使用有效的List之前已经完成了此操作,所以我可以假设创建一个新模型,但是我确信我应该能够对此进行反序列化。

I would greatly appreciate it if someone could suggest a way to deserialize this data or a better way to send the data in the first place. 如果有人可以建议一种反序列化此数据的方法,或者首先是一种更好的发送数据的方法,我将不胜感激。

MenuController: MenuController:

public JsonResult Populate(string id) {
    // Get and process data, creating all, a List<Dictionary<string,int>>
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = serializer.Serialize((object)all);
    return Json(json, JsonRequestBehavior.AllowGet);
}

I also tried the following instead of using the JavascriptSerielizer, but it had the same result: 我还尝试了以下方法而不是使用JavascriptSerielizer,但结果相同:

return Json(JsonConvert.SerializeObject(all), JsonRequestBehavior.AllowGet);

cshtml jquery: cshtml jquery:

<script type="text/javascript">
    $.get('@Url.Action("Populate","Menu")', { id: "MO" }, function (data) {
        console.log(data);
        var obj = jQuery.parseJSON(data);
        console.log('obj: ' + obj);
        for (var o in obj) {
            console.log('o is: ' + o);
        }    
    })
    .done(function (d) {
        console.log('done: ' + d);
    })
    .fail(function (d) {
        console.log('fail: ' + d);
    });
</script>

In the console I get: 在控制台中,我得到:

data 数据

[{"foo":2,"bar":0,"ray":3,"doh":1},{"mee":1,"so":0,"lah":2,"far":0}]

obj 对象

obj: [object Object],[object Object]

o Ø

o is: 0
o is: 1

done 完成

done: [{"foo":2,"bar":0,"ray":3,"doh":1},{"mee":1,"so":0,"lah":2,"far":0}]

Many thanks for your help. 非常感谢您的帮助。

So lists get translated to arrays and dictionaries to objects, so it seems that everything is working fine. 因此,列表被转换为数组,字典被转换为对象,因此看起来一切正常。 In Javascript, you have been handed an array with two objects in it. 在Javascript中,您已获得一个包含两个对象的数组。 You could iterate each object (which is the translated dictionary) as follows: 您可以如下迭代每个对象(即翻译后的字典):

var data;// your downloaded JSON object
for(var i = 0;i < data.length; ++i) //for enumerating array
{
     var obj = data[i];
     for(var propName in obj) //for enumerating the properties of an object
     {
         var value = obj[propName];
         alert("item : " + i + " : prop : " + propName + " : value : " + value);
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将Json反序列化为Dictionary <String,List<String> &gt; - How I can deserialize Json into Dictionary<String,List<String>> 无法将json反序列化为Dictionary <string, List<Purchase> &gt; - Can not deserialize json to Dictionary<string, List<Purchase>> 如何将JSON文档反序列化为Dictionary <string, List<object> &gt; - How do I deserialize a JSON document into a Dictionary<string, List<object>> 如何反序列化包含列表和字典(或键值)属性对象的json字符串 - How can I deserialize a json string contianing a list and dictionary (or keyvalue) property objects 如何转换清单 <string> 到字典 <string, int> ? - How can I transform a List <string> to a Dictionary<string, int>? 如何将JSON反序列化为简单的Dictionary <string,object> 使用动态的datagridview? - How can I deserialize JSON to a simple Dictionary<string,object> to datagridview using only dynamics? 无法将JSON反序列化为字典 <string, List<string> &gt;使用JavaScriptSerializer - Unable to deserialize JSON to a Dictionary<string, List<string>> using JavaScriptSerializer 如何序列化/反序列化为字典 <int,List<string> &gt;`来自自定义XML - How to serialize/deserialize to `Dictionary<int,List<string>>` from custom XML 将 JSON 反序列化为字典<string, List<string> &gt; - Deserialize JSON to Dictionary<string, List<string>> 如何从JSON反序列化[[int,int,int,int,string,string],[…]] - How to deserialize [[int,int,int,int,string,string], […]] from JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM