0

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.

Ruhul Amin
  • 15
  • 11
  • You need to make the properties collections - `public IEnumerable InfoList { get; set; }` and `public IEnumerable info { get; set; }` –  Oct 13 '16 at 07:19

3 Answers3

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; }
}
Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • Hello Sami. This code return s me bellow json format - > – Ruhul Amin Oct 13 '16 at 10:53
  • 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 ?? – Ruhul Amin Oct 13 '16 at 10:55
  • @Pongta That code will not return anything in json format so hard to say... – Sami Kuhmonen Oct 13 '16 at 11:00
  • 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. – Ruhul Amin Oct 15 '16 at 03:40
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; }
}
Community
  • 1
  • 1
Power Star
  • 1,724
  • 2
  • 13
  • 25
0

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

Deniz Kısır
  • 231
  • 3
  • 9