1

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"
    }               
              ]         
   }    
]
Liam
  • 27,717
  • 28
  • 128
  • 190
John
  • 35
  • 1
  • 5
  • Check my answer and tell me if something is not clear – mybirthname Dec 02 '15 at 14:09
  • 1
    Possible duplicate of [Turn C# object into a JSON string in .NET 4](http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4) – Liam Dec 02 '15 at 14:14
  • `var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(yourObject);` – Gene R Dec 02 '15 at 14:15

2 Answers2

3

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.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
1
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
mybirthname
  • 17,949
  • 3
  • 31
  • 55