如何从标题和细节类创建JSON[英] How to create Json from header and detail class

本文是小编为大家收集整理的关于如何从标题和细节类创建JSON的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是我设计并包含标题和细节的JSON课程,我正在使用Visual Studio 2015

public class SubDetail
    {
        public string Sub1 { get; set; }
        public string Sub2 { get; set; }
        public string Sub3 { get; set; }
    }

    public class RootObject
    {
        public string No { get; set; }
        public int Age { get; set; }
        public List<SubDetail> SubDetail { get; set; }
    }

以及如何通过使用Linq编写C#代码,然后将JSON放出如下

[
 {  
    "No":"1",
    "Age":7,
    "SubDetail":            
    [
    {   
      "Sub1":"1",
    "Sub2":"2",
    "Sub3":"3"
    },
    {
    "Sub1":"4",
    "Sub2":"5",
    "Sub3":"6"
    },
              { 
    "Sub1":"7",
    "Sub2":"8",
    "Sub3":"9"
    }               
              ]         
   }    
]

推荐答案

public class SubDetail
{
    public string Sub1 { get; set; }
    public string Sub2 { get; set; }
    public string Sub3 { get; set; }
}

public class RootObject
{
    public string No { get; set; }
    public int Age { get; set; }
    public List<SubDetail> SubDetail { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        RootObject obj = new RootObject();
        obj.No = "1";
        obj.Age = 7;

        int lenght = 3;
        int counter = 0;

        for(int i=0; i<lenght; i++)
        {
            SubDetail detail = new SubDetail();
            detail.Sub1 = (counter + 1).ToString();
            detail.Sub2 = (counter + 1).ToString();
            detail.Sub3 = (counter + 1).ToString();

            if (obj.SubDetail == null)
                obj.SubDetail = new List<SubDetail>();

            obj.SubDetail.Add(detail);
        }

        var jsonString = JsonConvert.SerializeObject(obj);

        Console.WriteLine(jsonString);

    }
}

这是您案件的完整示例.另外,您需要将newtonsoft.json dll添加到您的项目中,可以在软件包管理器控制台中使用此行进行操作.

Install-Package Newtonsoft.Json -Version 7.0.1

其他推荐答案

有许多数量的库来将对象图序列化为JSON格式.最受欢迎和易于使用的是 newtonsoft json.net 一行:

var jsonString = JsonConvert.SerializeObject(myRootObject);

这与LINQ无关,主要是因为您无需您探索您的子尾巴集合并手动生成JSON.使用标准,接受,测试解决方案.

本文地址:https://www.itbaoku.cn/post/1557041.html

问题描述

This is json class that I was design and contain header and also detail, and I'm using Visual studio 2015

public class SubDetail
    {
        public string Sub1 { get; set; }
        public string Sub2 { get; set; }
        public string Sub3 { get; set; }
    }

    public class RootObject
    {
        public string No { get; set; }
        public int Age { get; set; }
        public List<SubDetail> SubDetail { get; set; }
    }

and How to write c# code by using linq and get json out put like below

[
 {  
    "No":"1",
    "Age":7,
    "SubDetail":            
    [
    {   
      "Sub1":"1",
    "Sub2":"2",
    "Sub3":"3"
    },
    {
    "Sub1":"4",
    "Sub2":"5",
    "Sub3":"6"
    },
              { 
    "Sub1":"7",
    "Sub2":"8",
    "Sub3":"9"
    }               
              ]         
   }    
]

推荐答案

public class SubDetail
{
    public string Sub1 { get; set; }
    public string Sub2 { get; set; }
    public string Sub3 { get; set; }
}

public class RootObject
{
    public string No { get; set; }
    public int Age { get; set; }
    public List<SubDetail> SubDetail { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        RootObject obj = new RootObject();
        obj.No = "1";
        obj.Age = 7;

        int lenght = 3;
        int counter = 0;

        for(int i=0; i<lenght; i++)
        {
            SubDetail detail = new SubDetail();
            detail.Sub1 = (counter + 1).ToString();
            detail.Sub2 = (counter + 1).ToString();
            detail.Sub3 = (counter + 1).ToString();

            if (obj.SubDetail == null)
                obj.SubDetail = new List<SubDetail>();

            obj.SubDetail.Add(detail);
        }

        var jsonString = JsonConvert.SerializeObject(obj);

        Console.WriteLine(jsonString);

    }
}

Here is full example for your case. Also you need to add Newtonsoft.Json dll to your project you can do it with this line in the Package Manager Console.

Install-Package Newtonsoft.Json -Version 7.0.1

其他推荐答案

There are any number of libraries for serializing an object graph into JSON format. The most popular and easy to use is Newtonsoft JSON.NET, which will allow you to generate your JSON easily with one line:

var jsonString = JsonConvert.SerializeObject(myRootObject);

This has nothing to do with LINQ, mostly because there's no need for you to explore your SubDetail collection and generate the JSON manually. Use a standard, accepted, tested solution.