Using JSON.Net CustomCreationConverter



我正在尝试以这种格式反序列化 JSON:

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

到像这样的简单字典:

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

JSON.NET 4.0 中使用CustomCreationConverter

收到错误,说我只能反序列化为数组。这是对的吗?如何"强制"它创建字典?我需要创建自定义类吗?

试试看:

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
        }
    ]
});

相关内容

  • 没有找到相关文章

最新更新