简体   繁体   English

序列化嵌套json c#

[英]serializing nested json c#

Big apologies for the long post. 长期致歉。 I need to create the below json format for a post to rest api in c#. 我需要创建以下json格式,以便在c#中发布api。 The below call works and I have used successfully in Postman to add it to the target system. 以下调用有效,并且我已经在Postman中成功使用了将其添加到目标系统中。

{
"item": {
    "attrs": {
        "attr": [{
                "name": "IP_Category",
                "value": "Miscellaneous"
            }, {
                "name": "Description",
                "value": "Picture of Rabbit"
            }, {
                "name": "Title",
                "value": "A Rabbit"
            }
        ]
    },
    "resrs": {
        "res": [{
                "filename": "Rabbit.jpg",
                "base64": "/9j/4AAQSkZJR"
            }
        ]
    },
    "acl": {
        "name": "Submitter"
        },
       "entityName": "IP_Document"
    }
}

Based on the research I've done I need to copy and "paste special" into a new class file in visual studio so it can create the class objects based on the json format (Pretty cool!). 根据我已经完成的研究,我需要将其复制并“粘贴特殊”到Visual Studio中的新类文件中,以便它可以基于json格式创建类对象(非常酷!)。 And this is what it creates: 这就是它创建的:

namespace BasicWebApp
{

public class Rootobject
{
    public Item item { get; set; }
}

public class Item
{
    public Attrs attrs { get; set; }
    public Resrs resrs { get; set; }
    public Acl acl { get; set; }
    public string entityName { get; set; }
}

public class Attrs
{
    public Attr[] attr { get; set; }
}

public class Attr
{
    public string name { get; set; }
    public string value { get; set; }
}

public class Resrs
{
    public Re[] res { get; set; }
}

public class Re
{
    public string filename { get; set; }
    public string base64 { get; set; }
}

public class Acl
{
    public string name { get; set; }
}
}

Problem 1: Why is vs renaming the res json object to to class Re? 问题1:为什么vs将res json对象重命名为Re类? Is it a reserved word in c#? 它是C#中的保留字吗?

Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code? 问题2:我知道我必须以这种方式嵌套事物,但是我有两个层次,不确定要编码什么?

var model = new Rootobject();
model.item = new Item
{
    attrs = new Attrs
    {
        attr = new List<Attr>
            {
                 **now what??**
            }
    }
}

Don't have answer to your first question yet(but looking for it): 尚无第一个问题的答案(但正在寻找):

For your second question, at now what part you just do: 对于第二个问题, 现在您只需要做什么:

new Attr(){ name = "name1", value = "value1" },
new Attr(){ name = "name1", value = "value2" }

But that's not what i advise. 但这不是我的建议。 I advise you to have your Attr collection ready then assign it. 我建议您准备好Attr集合,然后进行分配。 Like: 喜欢:

var model = new Rootobject();
model.item = new Item
{
    attrs = yourAttrCollection
}

It also goes for everyting else you do. 它也适用于您所做的所有其他事情。 Have your stuff ready, then assign them. 准备好您的东西,然后分配它们。 It increases readability in nested objects. 它提高了嵌套对象的可读性。

Problem 1: Why is vs renaming the res json object to to class Re? 问题1:为什么vs将res json对象重命名为Re类? Is it a reserved word in c#? 它是C#中的保留字吗?

No Re is not reserved word in class. No Re不是课堂上的保留字。 As mentioned by TheGeneral 正如提将军所说

res is plural for re's res是re的复数

You can use JsonProperty attribute to resolve this issue like 您可以使用JsonProperty属性来解决此问题,例如

public class Resrs
{
    [JsonProperty("res")]
    public Re[] res { get; set; }
}

Problem 2: I know I have to nest things in this fashion but I two levels deep and not sure what to code? 问题2:我知道我必须以这种方式嵌套事物,但是我有两个层次,不确定要编码什么?

var model = new Rootobject();
model.item = new Item
{
    attrs = new Attrs
    {
        attr = new List<Attr>
            {
                 new Attr { name = "abc", value = "ABC" },
                 new Attr { name = "pqr", value = "PQR" }
            }
    }
}

Thanks to all that responded. 感谢所有回应。 It helped. 它有帮助。 I ended up using the below code to create the json object I needed. 我最终使用以下代码创建了所需的json对象。

var model = new RootObject();
            model.item = new Item
            {
                attrs = new Attrs
                {
                    attr = new List<Attr>
                {
                    new Attr { name = "IP_Category", value = ddlContent.Text },
                    new Attr { name = "Description", value = txtDesc.Text },
                    new Attr { name = "Title", value = txtTitle.Text }
                }
                },
                resrs = new Resrs
                {
                    res = new List<Re>
                {
                    new Re { filename = fName, base64 = base64string},
                }
                },
                acl = new Acl
                {
                    name = "Submitter"
                },
                entityName = "IP_Document"
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM