Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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.Net反序列化嵌套对象_C#_Json_Json.net - Fatal编程技术网

C# 使用Json.Net反序列化嵌套对象

C# 使用Json.Net反序列化嵌套对象,c#,json,json.net,C#,Json,Json.net,我正在处理一个返回如下内容的API: {"v1.ProjectList": { "self_link": { "href": "https://central-staged:8880/api/v1/projects", "methods": "GET" }, "items": [], "register": {"href": "https://central-staged:8880/api/v1/projects/register"

我正在处理一个返回如下内容的API:

{"v1.ProjectList": 
    { 
        "self_link": { "href": "https://central-staged:8880/api/v1/projects", "methods": "GET" }, 
        "items": [], 
        "register": {"href": "https://central-staged:8880/api/v1/projects/register", "methods": "POST"}, 
        "max_active": {"href": "https://central-staged:8880/api/v1/projects/max-active", "methods": "GET"} 
    }
}
我正在生成一组DTO以适应JSON字符串的反序列化

namespace v1
{
    public class Project
    {
        public rest_common.Link branches { get; set; }
        public float coordinate_x { get; set; }
        public float coordinate_y { get; set; }
        public string description { get; set; }
        public int id { get; set; }
        public string location { get; set; }
        public string name { get; set; }
        public rest_common.Link publish_events { get; set; }
        public rest_common.Link self_link { get; set; }
        public rest_common.Link thumbnail { get; set; }
        public System.Guid uuid { get; set; }
    }

    [JsonObject(Title = "v1.ProjectList")]
    public class ProjectList
    {
        public List<Project> items { get; set; }
        public rest_common.Link max_active { get; set; }
        public rest_common.Link register { get; set; }
        public rest_common.Link self_link { get; set; }
    }
}

namespace rest_common
{
    public class Link
    {
        public string href { get; set; }
        public string methods { get; set; }
    }
}
名称空间v1
{
公共类项目
{
公共rest_common.Link分支{get;set;}
公共浮点坐标_x{get;set;}
公共浮点坐标{get;set;}
公共字符串说明{get;set;}
公共int id{get;set;}
公共字符串位置{get;set;}
公共字符串名称{get;set;}
公共rest_common.Link发布_事件{get;set;}
公共rest\u common.Link self\u Link{get;set;}
公共rest_common.Link缩略图{get;set;}
public System.Guid uuid{get;set;}
}
[JsonObject(Title=“v1.ProjectList”)]
公共类项目列表
{
公共列表项{get;set;}
公共rest_common.Link max_active{get;set;}
公共rest_common.Link寄存器{get;set;}
公共rest\u common.Link self\u Link{get;set;}
}
}
名称空间rest\u公共
{
公共类链接
{
公共字符串href{get;set;}
公共字符串方法{get;set;}
}
}
该代码是从服务器端代码生成的,我可以根据需要修改它。目前我唯一不能修改的是服务器返回的JSON响应

我正试图找出如何反序列化此文件:

class Program
    {
        static void Main(string[] args)
        {
            var jsonContent = "{\"v1.ProjectList\": {\"self_link\": {\"href\": \"https://something.com/getblah\", \"methods\": \"GET\"}, \"items\": [], \"register\": {\"href\": \"https://something.com/postblah\", \"methods\": \"POST\"}, \"max_active\": {\"href\": \"https://something.com/getblah2\", \"methods\": \"GET\"}}}";
            var projectList = JsonConvert.DeserializeObject<ProjectList>(jsonContent);
            Console.ReadLine();
        }
    }
类程序
{
静态void Main(字符串[]参数)
{
var jsonContent=“{\”v1.ProjectList\”:{\“self\u link\”:{\“href\”:\”https://something.com/getblah\“,\”方法\“:\”获取\“},\”项目\“:[],\”注册\“:{\”href\“:\”https://something.com/postblah\“,\”方法\“:\”发布\“},\”最大活动\“:{\”href\“:\”https://something.com/getblah2\“,”方法“:\”获取“}}”;
var projectList=jsoninvert.DeserializeObject(jsonContent);
Console.ReadLine();
}
}

目前,
projectList
是正确类的实例,但其所有成员都是
null
您正在尝试反序列化一个对象,该对象包含一个名为
v1.projectList
的属性。因此,创建这样的类:

public class ProxyObject
{
    [JsonPropertyAttribute("v1.ProjectList")]
    public ProjectList ProjectList { get; set;}
}
。。然后这就行了:

var projectList = JsonConvert.DeserializeObject<ProxyObject>(jsonContent).ProjectList;
var projectList=jsoninvert.DeserializeObject(jsonContent.projectList);

属性
[JsonObject(Title=“v1.ProjectList”)]
是不必要的。

您试图反序列化一个对象,该对象包含一个名为
v1.ProjectList
的属性。因此,创建这样的类:

public class ProxyObject
{
    [JsonPropertyAttribute("v1.ProjectList")]
    public ProjectList ProjectList { get; set;}
}
。。然后这就行了:

var projectList = JsonConvert.DeserializeObject<ProxyObject>(jsonContent).ProjectList;
var projectList=jsoninvert.DeserializeObject(jsonContent.projectList);
属性
[JsonObject(Title=“v1.ProjectList”)]
是不必要的。

或者, 由于您正在创建反序列化JSON字符串的类的新实例,请指出:

var projectList = new (ProjectList)(JsonConvert.DeserializeObject<ProjectList>(jsonContent));
var projectList=new(projectList)(jsoninvert.DeserializeObject(jsonContent));
或者, 由于您正在创建反序列化JSON字符串的类的新实例,请指出:

var projectList = new (ProjectList)(JsonConvert.DeserializeObject<ProjectList>(jsonContent));
var projectList=new(projectList)(jsoninvert.DeserializeObject(jsonContent));

您应该粘贴一个非转义JSON示例。您应该粘贴一个非转义JSON示例。