Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am very new to web development and I am developing a web app using ASP.NET MVC 5.I have a requirement to deserialize a json string and store the values in C# Objects

This is how my JSON looks like
JavaScript
{
  "spellCheckQuery": "abc*",
  "searchStatus": "OK",
  "results": [
    {
      "id": "Enterprise 1 ",
      "attributes": [
        {
          "value": "Vertical",
          "id": "name",
          "label": "Name"
        },
        {
          "value": "Logical grouping of  products based on the physical properties and delivery mechanism of the product.  Verticals include: O ",
          "id": "infa_description",
          "label": "Description"
        }
      ],
      "fragments": [
        {
          "fragments": [
            "abc"
          ],
          "attrId": "name"
        },
        {
          "fragments": [
            "abc"
          ],
          "attrId": "infa_description"
        }
      ],
      "categoryIds": [
        "Enterprise "
      ]
    },
    {
      "id": "Enterprise 2",
      "attributes": [
        {
          "value": "Logical grouping of products based on patient condition. The 11 Franchises include",
          "id": "infa_description",
          "label": "Description"
        },
        {
          "value": "Franchise",
          "id": "name",
          "label": "Name"
        }
      ],
      "fragments": [
        {
          "fragments": [
            "abc"
          ],
          "attrId": "name"
        },
        {
          "fragments": [
            "abc"
          ],
          "attrId": "infa_description"
        }
      ],
      "categoryIds": [
        "Enterprise"
      ]
    }
  ],
  "originalQuery": "abc*",
  "resultCount": 2,
  "categoryDetail": [
    {
      "id": "Enterprise",
      "path": [
        "Enterprise Business Glossary",
        "abc"
      ],
      "description": [
        "abc Enterprise Business Glossary",
        "some text goes here"
      ]
    }
  ],
  "processingTime": 45
}



Below are the classes that I have created

C#
public class Attribute
{
    public string value { get; set; }
    public string id { get; set; }
    public string label { get; set; }
}

public class Fragment
{
    public List fragments { get; set; }
    public string attrId { get; set; }
}

public class Result
{
    public string id { get; set; }
    public List attributes { get; set; }
    public List fragments { get; set; }
    public List categoryIds { get; set; }
}

public class CategoryDetail
{
    public string id { get; set; }
    public List path { get; set; }
    public List description { get; set; }
}

public class RootObject
{
    public string spellCheckQuery { get; set; }
    public string searchStatus { get; set; }
    public List results { get; set; }
    public string originalQuery { get; set; }
    public int resultCount { get; set; }
    public List categoryDetail { get; set; }
    public int processingTime { get; set; }
}


I have used below code to deserialize:
C#
RootObject dobj = JsonConvert.DeserializeObject



My question is that the json has 2 result count with Ids enterprise 1 and Enterprise 2 respectively.I want to display these two result sets as my output. How can I achieve this? Any help would be greatly appreciated.

What I have tried:

C#
RootObject dobj = JsonConvert.DeserializeObject
Posted
Updated 16-Oct-16 21:58pm
v3

1 solution

dobj.results should be a list of two result objects so you can use that as a datasource for your objects, or show them in a view like

C#
@foreach(var result in Model.results)
{
    // your html here
    <h1>@result.id</h1>
}
 
Share this answer
 
Comments
Member 12794117 17-Oct-16 15:34pm    
That works . Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900