简体   繁体   English

使用JSON.Net CustomCreationConverter

[英]Using JSON.Net CustomCreationConverter

I'm trying to deserialize JSON in this format: 我正在尝试以这种格式反序列化JSON:

{
   "data": [
      {
         "installed": 1,
         "user_likes": 1,
         "user_education_history": 1,
         "friends_education_history": 1,
         "bookmarked": 1
      }
   ]
}

to a simple dictionary like this: 像这样的简单字典:

{
    "installed": true,
    "user_likes": true,
    "user_education_history": true,
    "friends_education_history": true,
    "bookmarked": true
}

using the CustomCreationConverter in JSON.NET 4.0 . JSON.NET 4.0使用CustomCreationConverter

I'm getting errors saying I can only deserialize to arrays. 我收到错误消息,说我只能反序列化到数组。 Is this correct? 这个对吗? How can I "force" it to create a dictionary? 如何“强制”创建字典? Do I need to create a custom class? 我需要创建一个自定义类吗?

Try it: 试试吧:

var convert = function(obj){
    var newObj = {};
    for(var prop in obj.data[0])
        newObj[prop] = obj.data[0][prop];
    return newObj;
}
convert({
    "data": [
        {
            "installed": 1,
            "user_likes": 1,
            "user_education_history": 1,
            "friends_education_history": 1,
            "bookmarked": 1
        }
    ]
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM