Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Json到自定义类_C#_Json - Fatal编程技术网

C#Json到自定义类

C#Json到自定义类,c#,json,C#,Json,我有: [ { "Name" : "SHA of timestamp", "ProjectURL" : "URL to Proj", "AccountIdentifier" : "Account Identifier", "Repositories": {

我有:

[
     {
        "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 config=JsonConvert.DeserializeObject(CONFIGJSON);
其中CONFIGJSON包含第一个json文件

提供ConfigEntry类的以下数据:

名称:“填充了一些数据”

ProjectURL:相同

AccountIdentifier:相同

存储库:NULL

如何用配置JSON中的所有数据填充Repositories字段


有什么想法吗。

看起来您可能不应该拥有
存储库
类-相反,请将
ConfigEntry.Repositories
属性更改为:

public Dictionary<string, Git> Repositories { get; set; }
公共字典存储库{get;set;}

然后,您将得到一个字典,其中“GetLink1”的键具有登录名为“login1”等的值。

看起来您可能不应该拥有
存储库
类-相反,请将
配置条目。存储库
属性更改为:

public Dictionary<string, Git> Repositories { get; set; }
公共字典存储库{get;set;}

然后,您将得到一个字典,其中“GetLink1”的键有一个登录名为“login1”的值,等等。

您的类定义不太正确

您的“repositories”类只包含一个Git,但需要是一个字典

我想如果你用这个你会很接近:

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; }
}
公共类Git
{
公共字符串登录{get;set;}
公共字符串密码{get;set;}
}
公共类配置条目
{
公共字符串名称{get;set;}
公共字符串ProjectURL{get;set;}
公共字符串AccountIdentifier{get;set;}
公共字典存储库{get;set;}
}
您可能还需要一个非泛型IEnumerable,以便它知道要实际创建哪些对象:

JsonConvert.DeserializeObject<List<ConfigurationEntry>>(CONFIGJSON);
JsonConvert.DeserializeObject(CONFIGJSON);

您的类定义不太正确

您的“repositories”类只包含一个Git,但需要是一个字典

我想如果你用这个你会很接近:

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; }
}
公共类Git
{
公共字符串登录{get;set;}
公共字符串密码{get;set;}
}
公共类配置条目
{
公共字符串名称{get;set;}
公共字符串ProjectURL{get;set;}
公共字符串AccountIdentifier{get;set;}
公共字典存储库{get;set;}
}
您可能还需要一个非泛型IEnumerable,以便它知道要实际创建哪些对象:

JsonConvert.DeserializeObject<List<ConfigurationEntry>>(CONFIGJSON);
JsonConvert.DeserializeObject(CONFIGJSON);