Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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,我正在使用这个类: class message { public content Content { get; set; } public from From { get; set; } public personalizations Personalizations { get; set; } } public class content { public string type = "te

我正在使用这个类:

 class message
    {
        public content Content { get; set; }
        public from From { get; set;  }
        public personalizations Personalizations { get; set; }
    }


    public class content
    {
        public string type = "text/html";
        public string value = "html";
    }

    public class from
    {
        public string email = "example@example.com";
        public string name = "example";

    }
    public class personalizations
    {
        public List<to> tos { get; set; }
    }
    public class to
    {
        public string subject { get; set; }
        public string email { get; set; }
    }
但我确实想要这种格式:

{
  "content": [
    {
      "type": "text/html", 
      "value": "Html"
    }
  ], 
  "from": {
    "email": "", 
    "name": ""
  }, 

  "personalizations": [
    {
      "subject": "",
      "to": [ { "email": "" }]
    },
    {
        "subject": "",
        "to": [{ "email": "" }]
    },
    {
        "subject": "",
        "to": [{ "email": "" }]
    }

    ]

}
如何将格式更改为最后一种格式

提前谢谢

编辑:

我想更改格式,而不是值 例如:


在上一个json示例中,我有一个personalization对象,其中包含多个json,但在第一个示例中,我只有一个对象

,根据您最近的评论,您希望更改输出格式。您可以通过将属性“publicstringsubject”从“to”类移动到“personalizations”类来实现这一点,如下所示:


顺便说一句,你应该看看

你可以把你想要的JSON复制到你的剪贴板上。然后,您可以从“编辑”菜单转到Visual Studio中的任何.cs文件,您可以展开“粘贴特殊”菜单。选择“将JSON粘贴为类”选项,您将得到以下结果:

public class Rootobject
{
    public Content[] content { get; set; }
    public From from { get; set; }
    public Personalization[] personalizations { get; set; }
}

public class From
{
    public string email { get; set; }
    public string name { get; set; }
}

public class Content
{
    public string type { get; set; }
    public string value { get; set; }
}

public class Personalization
{
    public string subject { get; set; }
    public To[] to { get; set; }
}

public class To
{
    public string email { get; set; }
}

null!=“
…请看@shurik您是对的。。。显然,OP知道如何从JSON构造类,以及如何将对象表示为单元素数组——重新打开并等待澄清它们到底有什么问题。是他试图处理的空值,还是JSON本身的结构?“个性化”在这两个示例中有不同的结构。@shurik不,我不是说值,而是说格式
{
  "content": [
    {
      "type": "text/html", 
      "value": "Html"
    }
  ], 
  "from": {
    "email": "", 
    "name": ""
  }, 

  "personalizations": [
    {
      "subject": "",
      "to": [ { "email": "" }]
    },
    {
        "subject": "",
        "to": [{ "email": "" }]
    },
    {
        "subject": "",
        "to": [{ "email": "" }]
    }

    ]

}
public class Rootobject
{
    public Content[] content { get; set; }
    public From from { get; set; }
    public Personalization[] personalizations { get; set; }
}

public class From
{
    public string email { get; set; }
    public string name { get; set; }
}

public class Content
{
    public string type { get; set; }
    public string value { get; set; }
}

public class Personalization
{
    public string subject { get; set; }
    public To[] to { get; set; }
}

public class To
{
    public string email { get; set; }
}