【问题标题】:serializing nested json c#序列化嵌套的json c#
【发布时间】:2018-11-12 05:56:23
【问题描述】:

对这篇长文深表歉意。我需要为帖子创建以下 json 格式以在 c# 中休息 api。以下调用有效,我已在 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"
    }
}

根据我所做的研究,我需要复制并“粘贴特殊”到 Visual Studio 中的新类文件中,以便它可以基于 json 格式创建类对象(非常酷!)。这就是它所创造的:

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; }
}
}

问题一:为什么 vs 将 res json 对象重命名为 Re 类?是c#中的保留字吗?

问题 2:我知道我必须以这种方式嵌套东西,但我有两层深,不知道要编码什么?

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

【问题讨论】:

  • res 不是保留字,但是您的 Json 到 C# 转换器(在本例中为 Visual Studio)认为 res 是复数re
  • 对于您的问题“res JSON object to class Re”,Visual Studio 将“Re”视为“Res”的单数形式。同样,您可以使用 Attr 和 Attrs 进行检查。
  • 对于您的问题 2,我个人的建议是使用第三方库,例如 Newtonsoft,这会让事情变得简单。

标签: c# json rest


【解决方案1】:

您的第一个问题还没有答案(但正在寻找):

对于您的第二个问题,现在您应该做什么:

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

但这不是我的建议。我建议您准备好 Attr 集合然后分配它。喜欢:

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

它也适用于你所做的一切。准备好你的东西,然后分配它们。它增加了嵌套对象的可读性。

【讨论】:

    【解决方案2】:

    问题一:为什么 vs 将 res json 对象重命名为 Re 类?是c#中的保留字吗?

    No Re 不是类中的保留字。 正如TheGeneral所提到的

    res 是 re's 的复数形式

    您可以使用 JsonProperty 属性来解决此问题,例如

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

    问题 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" }
                }
        }
    }
    

    【讨论】:

    • 我使用下面的代码来创建我需要的 json 结构。感谢所有回复:
    【解决方案3】:

    感谢所有回复。它有帮助。我最终使用下面的代码来创建我需要的 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"
                };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多