我有json:
[
{
"Name" : "SHA of timestamp",
"ProjectURL" : "URL to Proj",
"AccountIdentifier" : "Account Identifier",
"Repositories":
{
"GitLink1":
{
"Login" : "login1",
"Password" : "pas1"
},
"GitLink2":
{
"Login" : "login2",
"Password" : "pas2"
},
...
"GitLink999":
{
"Login" : "login999",
"Password" : "pass999"
}
}
},
...
{
Same entry
}
]
我需要在我创建的类的IEnumerable中填写它
public class Git
{
public string Login { get; set; }
public string Password { get; set; }
}
public class Repositories
{
public Git git { get; set; }
}
public class ConfigEntry
{
public string Name { get; set; }
public string ProjectURL { get; set; }
public string AccountIdentifier { get; set; }
public Repositories Repositories { get; set; }
}
使用
IEnumerable<ConfigurationEntry> config = JsonConvert.DeserializeObject<IEnumerable<ConfigurationEntry>>(CONFIGJSON);
CONFIGJSON包含第一个json文件。
给我这个ConfigEntry类的数据:
姓名:&#34;填写了一些数据&#34;
ProjectURL:相同
AccountIdentifier:相同
存储库:NULL
我如何使用配置JSON中的所有数据填充存储库字段?
有什么想法。
答案 0 :(得分:5)
看起来您可能不应该有Repositories
课程,而是将您的ConfigEntry.Repositories
属性更改为:
public Dictionary<string, Git> Repositories { get; set; }
然后你会得到一个字典,其中“GetLink1”的键的值为“login1”等。
答案 1 :(得分:0)
你的班级定义不太对。
您的“存储库”类只包含一个Git,但需要是一个Dictionary。
我认为如果你使用它,你将会很接近:
public class Git
{
public string Login { get; set; }
public string Password { get; set; }
}
public class ConfigEntry
{
public string Name { get; set; }
public string ProjectURL { get; set; }
public string AccountIdentifier { get; set; }
public Dictionary<string,Git> Repositories { get; set; }
}
您可能还需要一个非通用的IEnumerable,因此它知道要实际创建哪些对象:
JsonConvert.DeserializeObject<List<ConfigurationEntry>>(CONFIGJSON);