【发布时间】:2017-08-17 03:28:52
【问题描述】:
我想使用 c# 从 JSON 中获取“fruits”、“animals”、“numbers”,还想获取我正在使用的“fruits”、“animals”和“numbers”中的值json.net,但我不知道如何获得我想要的数据。
{
"fruits":[
"apple",
"orange",
"grapes",
"banana"
],
"animals":[
"cat",
"dog",
"lion",
"bird",
"horse"
],
"numbers":[
"1",
"2",
"3",
"4",
"5"
]
}
我知道我可以通过拥有这个来实现这一目标
public class RootObject
{
public List<string> fruits { get; set; }
public List<string> animals { get; set; }
public List<string> numbers { get; set; }
}
但是当我添加另一个像颜色这样的对象时,我需要添加
public List<string> colors{ get; set; }
我想要的只是简单地获取对象名称及其值,而无需定义新属性。
我知道这已经回答了,如果有的话,请评论一些链接。
【问题讨论】:
-
without having to create a new constructor构造函数是什么意思? -
为什么不反序列化为
Dictionary<string, List<string>>? -
@CodeNotFound 我的意思是不创建另一个公共列表
colors{ get;放; } -
@zerkms 我该怎么做?谢谢
-
@ExtendedAscii 你现在明白了 - 请先花一些时间自己尝试解决它。