Using JSON.Net CustomCreationConverter
本文关键字:CustomCreationConverter Net JSON Using | 更新日期: 2023-09-27 18:33:27
我正在尝试以这种格式反序列化 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
}
]
});