2

我正在尝试以这种格式反序列化 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
}

使用CustomCreationConverterin JSON.NET 4.0

我收到错误消息,说我只能反序列化为数组。它是否正确?如何“强制”它创建字典?我需要创建一个自定义类吗?

4

1 回答 1

-4

尝试一下:

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
        }
    ]
});
于 2012-05-26T11:33:26.040 回答