如何使用 newtonsoft json 序列化我的对象并给出整个结构?

问题描述 投票:0回答:1

我正在尝试使用 newtonsoft 打印出我的对象的 json 结构。例如:-

class details1{
 public string var1;
 public list<details2> list1;
}

class details2{
 public string var2;
 public list<details3> list2;
}

class details3{
 public string var3;
}

我需要的输出:

{
 "var1":"",
 "list1":
 [
  {
   "var2":"",
   "list2":
   [
     {
      "var3":""
     }
   ]
  }
 ]
}

我得到的输出:

{
 "var1":null,
 "list1":null
}

我做错了什么?

尝试使用 JsonConvert.Serialize 和 JsonSerializer 两者,但结果相同。

我能够让它显示详细信息1的空列表,即列表1,但不显示该列表的内容,即详细信息2。

class details1{
 public details1(){
  list1=new List<details2>();
 }
 public string var1;
 public list<details2> list1;
}

输出:

{
 "var1":null,
 "list1":[]
}
c# json.net
1个回答
0
投票

它不能按你想要的方式处理列表...... 试图向您展示示例:

void Main()
{
    var x = new first();
    //x.lst.AddRange(new List<thirdClass>(){ new thirdClass() {third1="xxx"}});
    JsonConvert.SerializeObject(x, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings() {NullValueHandling = NullValueHandling.Include} ).Dump();
}

public class first { 
    public string first1 {get;set;}
    public string first2 {get;set;}
    public string first3 { get; set; }
    public secondClass second1 { get; set; }
    //[JsonProperty(Required = Required.Default, PropertyName = "lst", DefaultValueHandling = DefaultValueHandling.Include, NullValueHandling = NullValueHandling.Include)]
    public List<thirdClass> lst {get;set;}
    public first() {second1 = new secondClass(); lst = new List<thirdClass>();}
}

public class secondClass{
    public string second1 { get; set; }
    public string second2 {get;set;}
    public thirdClass third2 {get;set;}
    public secondClass(){
        third2 = new thirdClass();
    }
}
public class thirdClass
{
    public string third1 {get;set;}
}

结果会是

{
  "first1": null,
  "first2": null,
  "first3": null,
  "second1": {
    "second1": null,
    "second2": null,
    "third2": {
      "third1": null
    }
  },
  "lst": []
}

如果取消注释第二行 - 它将序列化列表对象

{
  "first1": null,
  "first2": null,
  "first3": null,
  "second1": {
    "second1": null,
    "second2": null,
    "third2": {
      "third1": null
    }
  },
  "lst": [
    {
      "third1": "xxx"
    }
  ]
}

另外,你可以将我尝试过的称为 NJsonSchema (nuget)

NJsonSchema.JsonSchema.FromType(typeof(first)).ToSampleJson().ToString()

它可能会满足您的期望.. 和你的课程,(这很奇怪,但对我来说有点用)

  • 所有变量和类名必须采用驼峰式命名法

  • 应该是属性(而不是字段)

  • 并且为第一次入侵创建了架构(奇怪)

{
  "var1": "var1",
  "list1": [
    {
      "var2": "var2"
    }
  ]
}

我作为 Newtonsoft 模式生成器所做的另一次尝试 - 创建了正确的模式

JSchemaGenerator generator = new JSchemaGenerator();
    //generator.SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName;
    //generator.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
    //generator.GenerationProviders.Add(new StringEnumGenerationProvider());
    generator.Generate(typeof(Details1)).ToString().Dump();

--

{
  "definitions": {
    "Details2": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Var2": {
          "type": [
            "string",
            "null"
          ]
        },
        "List2": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "$ref": "#/definitions/Details3"
          }
        }
      },
      "required": [
        "Var2",
        "List2"
      ]
    },
    "Details3": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "Var3": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "required": [
        "Var3"
      ]
    }
  },
  "type": "object",
  "properties": {
    "Var1": {
      "type": [
        "string",
        "null"
      ]
    },
    "List1": {
      "type": [
        "array",
        "null"
      ],
      "items": {
        "$ref": "#/definitions/Details2"
      }
    }
  },
  "required": [
    "Var1",
    "List1"
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.