如何反序列化嵌套字典?
我正在编写一个 Windows Phone 应用程序,它将从服务器获取一组数据并在列表框中显示每个条目。
数据来自服务器,看起来像这样
{
"data": [
{
"value1": 777103069782066,
"value2": "SomeString",
"value3": "TextToDisplay1"
},
{
"value1": 750050696652932,
"value2": "SomeString2",
"value3": "TextToDisplay2"
},
{
"value1": 516092242936133,
"value2": "SomeString3",
"value3": "TextToDisplay3"
}
]
}
然后我像这样反序列化数据
Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
问题是我的所有值都被混入一个字典条目
有人能告诉我我做错了什么吗?
谢谢,
克里斯
I am writing a Windows Phone app that will get a set of data from a server and display each entry in a list box.
The data comes from the server looking like this
{
"data": [
{
"value1": 777103069782066,
"value2": "SomeString",
"value3": "TextToDisplay1"
},
{
"value1": 750050696652932,
"value2": "SomeString2",
"value3": "TextToDisplay2"
},
{
"value1": 516092242936133,
"value2": "SomeString3",
"value3": "TextToDisplay3"
}
]
}
Then I deserialize the data like this
Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
The problem is that all my values get mashed into one dictionary entry
Could someone tell me what I am doing wrong?
Thanks,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dictionary
应该每个值都有一个唯一的键,在您的示例中,所有项目都具有相同的键(value1、value2、value3)。这就是为什么你的所有值都会被整合到一个字典条目中。Dictionary<string,object>
are supposed to have a unique key per value, in your example, all the items have the same keys (value1,value2,value3). That's why all your values get mashed into one dictionary entry.看来您无意中通过包含单个对象的键“data”创建了一个包含一个对象的字典,这是一个字典列表?
It appears that you are unintentionally making a dictionary containing one object by the key "data" containing a single object, which is a list of dictionaries?