c# - C# 将 JSON 序列化为简单的字典
问题描述
我有以下json:
{
"Name" : "Value1",
"Data": [
{
"UserName": "test",
"email": "test@test.com",
"UserInformation": {
"City": "city",
"country:": "country"
}
},
{
"UserName": "test1",
"email": "test1@test.com"
}
]
}
我想要下一本字典:
{
"Name" : "Value1",
"Data[0].UserName": "test",
"Data[0].email": "test@test.com",
"Data[0].UserInformation.City": "city",
"Data[0].UserInformation.country": "country",
"Data[1].UserName": "test1",
"Data[1].email": "test1@test.com"
}
使用 Newtonsoft.Json 无助于解决我的问题。拜托,你能帮帮我吗?
解决方案
如果我理解正确,您正在寻找这样的东西(使用 Newtonsoft.Json):
var dictionary = JObject.Parse(json)
.Descendants()
.OfType<JValue>()
.ToDictionary(jv => jv.Path, jv => jv.ToString());
推荐阅读
- oracle - Mybatis,检索存储为CLOB spring boot的oracle字段
- graphics - 带有平方衰减的阴影总是使一切变黑
- react-native - 使用 Expo 运行的 React Native 应用程序实际上可以通过 Detox/Jest 测试吗?
- spring - Tomcat Session 和 Oracle Database 连接如何在 Java Spring Web 应用程序中相互交互?
- excel - 向宏添加插入文件名函数
- python - 不明白为什么会出现这个“OverflowError: Python int too large to convert to C long”异常
- amazon-web-services - 如何确定哪个策略/角色正在授予对 S3 存储桶的访问权限
- angular - 找不到模块'webpack'
- r - 使用 dbSendQuery 在数据库上创建表时避免警告消息“有一个结果对象仍在使用中”
- android - 使用自定义对象的ArrayList和Gson恢复RecyclerView状态时ArrayIndexOutOfBoundsException的解决方法