-1

How can I do JSON in C# like the data below ?

{
    "Aliases": [ "teddy", "freddy", "eddy", "Betty" ], 
    "Name":"reacher gilt", 
    "Address":"100 East Way", 
    "Age":74, 
    "Bars": { 
        "items": [ 
            {
                "Sub_Property1":"beep", 
                "Sub_Property2":"boop" 
            },
            {
                "Sub_Property1":"meep",
                "Sub_Property2":"moop"
            },
            {
                "Sub_Property1":"feep",
                "Sub_Property2":"foop"
            }
        ]
    }
}

Actually my problem is inside the sub-collection. I saw someone did something like this

person.Bars.Add("items",
        new List<BarClass>(new[]{ 
        new BarClass("beep","boop"),
        new BarClass("meep","moop"),
        new BarClass("feep","foop"),
    }));

So, I have to add new BarClass("beep","boop"), but I need to do something like this

String [] no1 = {1,2,3}
String [] no2 = {4,5,6}
person.Bars.Add("items",
    new List<BarClass>(new[]{                 
    for ()
        {
           new BarClass(no1[i],no2[i])
        }      
}));

How can i do this? Thanks and please help..

Arghya C
  • 9,805
  • 2
  • 47
  • 66
John
  • 35
  • 1
  • 5

3 Answers3

0

You should create some classes

public class Person
{
 public string Name {get;set;}    
 public string Address{get;set;}
 public int Age {get;set;}
 public Header {get;set;}
}    

public class Header
{
 public Detail[] Details {get;set;}
}

public class Detail
{
 public string Sub1 {get;set;}
 public string Sub2 {get;set;}
}

Create instance from Person class and initialize to instance after than

JavaScriptSerializer serializer =new JavaScriptSerializer();
var result=serializer.Serialize(instanceOfPerson);

"result" is json data

Fatih Sert
  • 151
  • 1
  • 4
0

Assuming that you mean you want to create JSON string, you need to create those classes and use something like Newtonsoft JSON.net:

public class Item
{
    public string Sub_Property1 { get; set; }
    public string Sub_Property2 { get; set; }
}

public class Bars
{
    public List<Item> items { get; set; }
}

public class Person
{
    public List<string> Aliases { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
    public Bars Bars { get; set; }
}

Please read the documentation here: http://www.newtonsoft.com/json/help/html/Introduction.htm

Felix Av
  • 1,254
  • 1
  • 14
  • 22
0

To read the JSON

The best way to read the whole JSON is to Deserialize it to a native C# object. If you do not already have the classes with your, you can create it in Visual Studio as

  • Copy your JSON text
  • Create a new empty class file in VS
  • Edit > Paste Special > Paste JSON As Classes

Here are the classes

public class Person
{
    public string[] Aliases { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
    public Bars Bars { get; set; }
}

public class Bars
{
    public Item[] items { get; set; }
}

public class Item
{
    public string Sub_Property1 { get; set; }
    public string Sub_Property2 { get; set; }
}

Now you can use some .NET JSON library to deserialize. JSON.Net aka Newtonsoft JSON is a great library. You get get it from NuGet as well.

Then it's pretty easy to get the C# object from the JSON

//using Newtonsoft.Json;
var jsonString = File.ReadAllText(@"C:\YourDirectory\person.json");
var person = JsonConvert.DeserializeObject<Person>(jsonString);

If you want to read the sub-collection only, you can rather use Linq-to-JSON to read the items directly, like this

//using Newtonsoft.Json.Linq;
var jObject = JObject.Parse(jsonString);
List<Item> details = jObject["Bars"]["items"].ToObject<List<Item>>();

To create the JSON

You first need to create the object, then Serialize to JSON

string[] subProperties1 = new string[] { "1", "2", "3" };
string[] subProperties2 = new string[] { "4", "5", "6" };

Person person = new Person { Name = "Johny", Age = 7, Address = "Earth", Aliases = new string[] { "Sony", "Monty" } };
person.Bars = new Bars { 
    items = subProperties1.Zip(subProperties2, 
                                (prop1, prop2) => new Item { Sub_Property1 = prop1, Sub_Property2 = prop2 })
                            .ToArray() };
var json = JsonConvert.SerializeObject(person);

To create the items from your existing string arrays, I have used IEnumerable.Zip function from Linq. You can read about them here.

This is the created JSON data

{
  "Aliases": [ "Sony", "Monty" ],
  "Name": "Johny",
  "Address": "Earth",
  "Age": 7,
  "Bars": {
    "items": [
      {
        "Sub_Property1": "1",
        "Sub_Property2": "4"
      },
      {
        "Sub_Property1": "2",
        "Sub_Property2": "5"
      },
      {
        "Sub_Property1": "3",
        "Sub_Property2": "6"
      }
    ]
  }
}
Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • Hello Arghya please me another issue please. http://stackoverflow.com/questions/34044127/json-pare-sub-collection – John Dec 02 '15 at 14:04