将嵌套的json反序列化为C#对象并访问对象 [英] Deserializing nested json to C# objects and accessing objects

查看:79
本文介绍了将嵌套的json反序列化为C#对象并访问对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是Web开发的新手,我正在使用ASP.NET MVC开发一个Web应用程序5.我需要对json进行反序列化字符串并将值存储在C#对象中



这就是我的JSON的样子

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

{
  "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

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:

RootObject dobj = JsonConvert.DeserializeObject







我的问题是json有2个结果计数我分别使用Ids enterprise 1和Enterprise 2.我想将这两个结果集显示为输出。我怎样才能做到这一点?任何帮助将不胜感激。



我尝试过:






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:

RootObject dobj = JsonConvert.DeserializeObject

推荐答案

dobj.results应该是两个结果对象的列表所以你可以把它作为对象的数据源,或者在一个视图中显示它们,比如



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

@foreach(var result in Model.results)
{
    // your html here
    <h1>@result.id</h1>
}


这篇关于将嵌套的json反序列化为C#对象并访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆