【发布时间】:2017-03-23 14:16:50
【问题描述】:
我有一个 CSV 字符串,其中一个列值是 json 序列化的。
"Id,Name,Seo\r\n13,SpecialCollections,\"{\"\"SeoUrl\"\":\"\"special-collections\"\",\"\"SeoPageTitle\"\":null,\"\"SeoKeywords\"\":null,\"\"SeoDescription\"\":null}\"\r\n";
我正在使用JSON.NET 和ServiceStack.Text 的组合从json-csv 序列化和反序列化我的数据,反之亦然。
所以,有了上面的CSVinput,我首先使用ServiceStack.Text辅助方法将其转换为.NET对象
var obj= csvInput.FromCsv<List<dynamic>>();
这给了我以Dictionary<string,string> 形式输出的每一行csv
1) {[Id, 13]}
2) {[Name, SpecialCollections]}
3) {[Seo, {"SeoUrl":"special-collections","SeoPageTitle":null,"SeoKeywords":null,"SeoDescription":null}]}
然后我用 JSON.NET 辅助方法将上面的输出序列化并写入一个看起来像这样的文件
var serializedJson = JsonConvert
.SerializeObject(obj, Formatting.Indented);
结果
[
{
"Id": "13",
"Name": "SpecialCollections",
"Seo": "{\"SeoUrl\":\"special-collections\",\"SeoPageTitle\":null,\"SeoKeywords\":null,\"SeoDescription\":null}"
}
]
问题在于嵌套属性“Seo”,虽然它的值是序列化的 json,但这是因为它是 string、JSON.NET 被视为 string 并且不格式化。无论如何,我可以获得以下预期的结果?
预期结果:
[
{
"Id": "13",
"Name": "SpecialCollections",
"Seo": {
"SeoUrl": "special-collections",
"SeoPageTitle": null,
"SeoKeywords": null,
"SeoDescription": null
}
}
]
对此的任何帮助将不胜感激。
【问题讨论】:
标签: json.net servicestack-text