0

I am trying to create or searealize json in this format

   {

     "title": "Star Wars",
    "link": "http://www.starwars.com",
    "description": "Star Wars blog.",
    "item": [
       {
         "title": "Episode VII",
         "description": "Episode VII production",
         "link": "episode-vii-production.aspx"
        },
        {
         "title": "Episode VITI",
         "description": "Episode VII production",
         "link": "episode-vii-production.aspx"
        }


        ]
       }

i am trying this achieve this

 dynamic o = new ExpandoObject();

o.title= "fdsfs";
o.link= "fsrg";
o.description="fdsfs";
      foreach (var adata in all)
                {
                o.item.title="fgfd";
                o.item.description="sample desc";
                o.item.link="http://google.com"
                } 
 string json = JsonConvert.SerializeObject(o);

but than here it throws exception on foreach loop on item it tells it does not contain defination for the same etc .so what i am doing wrong and how to achieve the same

2 Answers 2

2

That is the construction you should have to get the json you've stated. The problem with your code is that item should be actually a list of items.

public class Item
{
    public string title { get; set; }
    public string description { get; set; }
    public string link { get; set; }
}

public class RootObject
{
    public string title { get; set; }
    public string link { get; set; }
    public string description { get; set; }
    public List<Item> item { get; set; }
}

Then you ca use this code:

 dynamic o = new ExpandoObject();

o.title= "fdsfs";
o.link= "fsrg";
o.description="fdsfs";
o.item = new List<ExpandoObject>();  
//although list of dynamics is not recommended as far as I remember
      foreach (var adata in all)
                {
                o.item.Add(new Item(){   
                title="fgfd",
                description="sample desc",
                link="http://google.com" });
                } 
 string json = JsonConvert.SerializeObject(o);
Sign up to request clarification or add additional context in comments.

4 Comments

i want achieve it dynamically not using class, so is it possible ?
as long as you create a list of items (even if dynamicly), you're good to go
@FelixAv You add items to o.item without creating a list.
@Eser Thanks for the heads up
1

You have to create o.item to assign values into it:

dynamic o = new ExpandoObject();
var all = new object[] { new object() };

o.title= "fdsfs";
o.link= "fsrg";
o.description="fdsfs";
var items = new List<ExpandoObject>();
foreach (var adata in all)
{
    dynamic item = new ExpandoObject();
    item.title="fgfd";
    item.description="sample desc";
    item.link="http://google.com";
    items.Add(item);
} 
o.item = items;
string json = JsonConvert.SerializeObject(o);

1 Comment

shouldn't o.item be a list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.