1

Problem Specification: Build a web api in asp.net to post multi level json formatted data.

Json Format:

{
  "InfoList:"[
    {
    "Name": " Sample String 1",
    "URL": " Sample String2",
    "Info":[
        {
            "ZIP": " Sample String3 "
            "status": "Sample String4"
        }
    ]
     }
   ]
}

I tried to solve this problem. My models are given bellow:

My Models:

public class Information : InfoList
    {
        public InfoList InfoList { get; set; }
    }

public class InfoList 
        {

            public string Name { get; set; }

            public string URL { get; set; }

            public Info info { get; set; }
        }

public class Info
    {
        public string ZIP{ get; set; }

        public string status { get; set; }

    }

Those models Generates this kind of Json Format:

{
  "InfoList": {
    "Name": "sample string 1",
    "URL": "sample string 2",
    "Info": {
      "ZIP": "sample string 1",
      "status": "sample string 2"
    }
  },
  "Name": "sample string 1",
  "URL": "sample string 2",
  "Info": {
    "ZIP": "sample string 1",
    "status": "sample string 2"
  }
}

I need those parentheses exactly like the problem specification.What to do? I am working with ASP.Net Web API controller.

1
  • You need to make the properties collections - public IEnumerable<InfoList> InfoList { get; set; } and public IEnumerable<Info> info { get; set; } Commented Oct 13, 2016 at 7:19

3 Answers 3

1

You're inheriting Information from InfoList so naturally it will get the fields Name, URL etc. Remove the inheritance and change the InfoList and Info members into arrays or lists and you get what you want.

public class Information
{
    public InfoList[] InfoList { get; set; }
}

public class InfoList 
{
    public string Name { get; set; }
    public string URL { get; set; }
    public Info[] info { get; set; }
}

public class Info
{
    public string ZIP{ get; set; }
    public string status { get; set; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Sami. This code return s me bellow json format - >
This code is not giving me the exact Json format. My Post Method: public string Post([FromBody]Information value) { string hello; return "value"; } Is there any problem with my post method ??
@Pongta That code will not return anything in json format so hard to say...
Sorry for my wrong comment. Basically I tried to say something else. By the way my problem is solved. Your classes works properly. Thank you.
0

You just copy your JSON then create C# Class file. Then click Edit -> Paste Special -> Paste JSON as Classes. Thats it...

You need Visual Studio for this.

For more information please see below link

How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special?

Valid JSON

{
    "InfoList":  {
        "Name": " Sample String 1",
        "URL": " Sample String2",
        "Info": [
            {
                "ZIP": " Sample String3 ",
                "status": "Sample String4"
            }
        ]
    }
}

So your class must be like this.

public class Rootobject
{
    public Infolist InfoList { get; set; }
}

public class Infolist
{
    public string Name { get; set; }
    public string URL { get; set; }
    public Info[] Info { get; set; }
}

public class Info
{
    public string ZIP { get; set; }
    public string status { get; set; }
}

1 Comment

You might want to mention that this needs Visual Studio. It's not anything inherent for C#.
0

You can use http://json2csharp.com/ online tool to create c# class from json data string. Just copy your json and get classes.

Comments

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.