C# 如何设置复杂Json负载的属性及其值并进行序列化?

C# 如何设置复杂Json负载的属性及其值并进行序列化?,c#,.net,json,api,serialization,C#,.net,Json,Api,Serialization,我的框架中有以下结构。我有一个助手,其中有以下属性代码、解析器代码和序列化方法。在问题的另一部分与此链接 我现在有了任何一个API,它接受一个根对象很大的负载。我如何使用SerializeForApiMethod方法进行此操作,因为它具有根对象。我试图为这个Jsonpayload在同一个模型对象引用中设置值 //下面是Json模式 { "tagNode": { "query": null, "type": 0, "filter": null, "ldapPat

我的框架中有以下结构。我有一个助手,其中有以下属性代码、解析器代码和序列化方法。在问题的另一部分与此链接

我现在有了任何一个API,它接受一个根对象很大的负载。我如何使用SerializeForApiMethod方法进行此操作,因为它具有根对象。我试图为这个Jsonpayload在同一个模型对象引用中设置值

//下面是Json模式

{
  "tagNode": {
    "query": null,
    "type": 0,
    "filter": null,
    "ldapPaths": null,
    "editPermissions": 0,
    "id": 0,
    "disallowed": false,
    "name": "NewTag"
  },
  "parentId": 0
}

//Model class
public class TagModel
{
   public static TagModel mode = new TagModel
   {
            endpointIds = new List<int> { -2147483612, -2147483611 },
            tagIds = new List<int> { 35, 37 }, 
            id = -2147483639,
            parentId = 37,
            nodeId = 1,
            oldParentId = null,
            isEndpoint = false,
            state = 2,
            destinationTag = 2,



   };

    //This Object I am Confused on how to Serialize as above schema it in the 
   //same class whichI w ant to set the values inside the TagModel instance. 
    //Also has same attributes like id and parentId used already in the 
    //other methods and also this complex payload has a class TagNode which is 
    //the root object. 
     [UseWithApiMethods("UpdateTag")]
     public TagNode tagNode { get; set; }
     public object query { get; set; }
     public int type { get; set; }
     public object filter { get; set; }
     public object ldapPaths { get; set; }
     public int editPermissions { get; set; }
     public int id { get; set; }
     public bool disallowed { get; set; }
     public string name { get; set; }
     public int parentId { get; set; }



             //For this Object I am able to serialize it
           [UseWithApiMethods("UpdateTagToRoot")]
            public int nodeId { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public object oldParentId { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public bool isEndpoint { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public int state { get; set; }
            [UseWithApiMethods("UpdateTagToRoot")]
            public int destinationTag { get; set; }



              //For this Object I am able to serialize it
            [UseWithApiMethods("UpdateEndpointsToTags")]
            public List<int> endpointIds { get; set; }
            [UseWithApiMethods("UpdateEndpointsToTags")]
            public List<int> tagIds { get; set; }


          //For this Object I am able to serialize it
           [UseWithApiMethods("UpdateEndpointsFromTags")]
           public int id { get; set; }
           [UseWithApiMethods("UpdateEndpointsFromTags")] 
           public int parentId { get; set; }


}

}

{
“标记节点”:{
“查询”:空,
“类型”:0,
“过滤器”:空,
“ldappath”:空,
“编辑权限”:0,
“id”:0,
“不允许”:错误,
“名称”:“新标签”
},
“父ID”:0
}
//模范班
公共类标记模型
{
公共静态TagModel模式=新TagModel
{
EndpointId=新列表{-2147483612,-2147483611},
tagIds=新列表{35,37},
id=-2147483639,
parentId=37,
nodeId=1,
oldParentId=null,
isEndpoint=false,
状态=2,
destinationTag=2,
};
//对于这个对象,我不知道如何按照上面的模式序列化它
//同一个类,它需要在TagModel实例中设置值。
//还具有相同的属性,如已在中使用的id和parentId
//其他方法以及这个复杂的负载都有一个类TagNode,它是
//根对象。
[使用APImethods(“更新标签”)]
公共标记节点标记节点{get;set;}
公共对象查询{get;set;}
公共int类型{get;set;}
公共对象筛选器{get;set;}
公共对象ldappath{get;set;}
公共int编辑权限{get;set;}
公共int id{get;set;}
公共布尔不允许{get;set;}
公共字符串名称{get;set;}
public int parentId{get;set;}
//对于这个对象,我可以序列化它
[与APImethods一起使用(“updateAgtoroot”)]
公共int节点ID{get;set;}
[与APImethods一起使用(“updateAgtoroot”)]
公共对象oldParentId{get;set;}
[与APImethods一起使用(“updateAgtoroot”)]
公共布尔isEndpoint{get;set;}
[与APImethods一起使用(“updateAgtoroot”)]
公共int状态{get;set;}
[与APImethods一起使用(“updateAgtoroot”)]
公共int destinationTag{get;set;}
//对于这个对象,我可以序列化它
[与APImethods一起使用(“UpdateEndpointsToTags”)]
公共列表端点ID{get;set;}
[与APImethods一起使用(“UpdateEndpointsToTags”)]
公共列表标记ID{get;set;}
//对于这个对象,我可以序列化它
[与APImethods一起使用(“UpdateEndpointsFromTags”)]
公共int id{get;set;}
[与APImethods一起使用(“UpdateEndpointsFromTags”)]
public int parentId{get;set;}
}
}

要使用自定义解析器从另一个问题中获取所需的JSON负载,您需要类结构如下所示:

public class TagModel
{
    [UseWithApiMethods("UpdateTag")]
    public TagNode tagNode { get; set; }

    public class TagNode
    {
        [UseWithApiMethods("UpdateTag")]
        public object query { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int type { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public object filter { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public object ldapPaths { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int editPermissions { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int id { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public bool disallowed { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public string name { get; set; }
    }

    [UseWithApiMethods("UpdateEndpointsFromTags", "UpdateTag")]
    public int parentId { get; set; }

    ... (other properties for your other API methods) ...
}
    [UseWithApiMethods("UpdateEndpointsFromTags", "UpdateTag")]
    public int parentId { get; set; }
请注意,您必须将
[UseWithApiMethods(“UpdateTag”)]
放在根类的
tagNode
属性上,以及要包含在JSON中的
tagNode
子类中的所有属性上。(这是必需的,因为解析器将只包含您用
[UseWithApiMethods]
属性专门标记的属性。因此,如果您想全部包含这些属性,则需要将它们全部标记)

对于
parentId
属性,您已经将其用于
UpdateEndpointsFromTags
方法,但这没有问题;您还可以将其用于其他方法,如
UpdateTag
。只需将方法名称添加到
[UseWithApiMethods]
属性中,如下所示:

public class TagModel
{
    [UseWithApiMethods("UpdateTag")]
    public TagNode tagNode { get; set; }

    public class TagNode
    {
        [UseWithApiMethods("UpdateTag")]
        public object query { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int type { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public object filter { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public object ldapPaths { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int editPermissions { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public int id { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public bool disallowed { get; set; }

        [UseWithApiMethods("UpdateTag")]
        public string name { get; set; }
    }

    [UseWithApiMethods("UpdateEndpointsFromTags", "UpdateTag")]
    public int parentId { get; set; }

    ... (other properties for your other API methods) ...
}
    [UseWithApiMethods("UpdateEndpointsFromTags", "UpdateTag")]
    public int parentId { get; set; }
这是一把小提琴,它展示了:


希望这就是您要查找的内容并解决您的困惑。

为什么要粘贴图像?它们不仅令人讨厌,而且事与愿违——我们无法复制和粘贴其中的数据,搜索引擎也无法索引数据content@MichaelRandall我已经删除了图像,并将有效负载与object放在一起。您打算将此模型与哪些API方法一起使用?哪些属性应该用哪些API方法序列化?我看不到任何东西会阻止您在这个复杂模型上使用
[UseWithApiMethods]
属性,而不是在简单模型上使用。你到底对什么感到困惑?@BrianRogers我对如何使用我拥有的“TagModel”相同的模型类实现它感到困惑。另外,因为我在另一个负载中使用了一些属性。它确实允许我再次使用这些相同的,除非我将它们重命名为id和parentId。我可以用另一种方式分配它们吗。我更新了关于我试图实现什么的问题。
TagNode
类的定义是什么?确定是有意义的。如果我想在“UpdateTag”和“UpdateEndpointsFromTags”中为parentId使用不同的值,该怎么办。目前我已经设定到37岁。但我想把它设为另一个数字。不影响“UpdateEndpointsFromTags”方法。这样做的原因是我想在一次单击中运行它们,然后创建一个新的TagModel实例,并将属性设置为您需要的任何值。你看我的回答中有什么不对劲。这正好说明我在做。是的,我检查了小提琴。这正是我试图做的,但无法在类下使用相同的引用名称“model”创建,这是不允许的。它抛出一个错误“该类已包含模型的定义”。我遗漏了什么吗?您是否正在尝试在TagModel类中创建多个具有相同属性名
model
的TagModel静态实例?如果是这样,你就不能那样做。或者为静态属性指定不同的名称