如何用c#解析嵌套的JSON

时间:2017-06-27 18:58:48

标签: c# json

这是JSON的布局

{
    "Key": "Standard Input",
    "Value": [
        {
            "code": "2102012000",
            "attributes": {
                "Tier 3 Description": "Liquid Waste",
                "Activity Value Required?": "No",
                "Tier 3 Code": "02",
                "Sector": "Fuel Comb - Industrial Boilers, ICEs - Other",
                "Tier 2 Code": "04",
                "SCC Level Four": "Total",
                "Tier 1 Description": "Fuel Comb. Industrial",
                "SCC Level One": "Stationary Source Fuel Combustion",
                "Status": "Active",
                "Tier 2 Description": "Other",
                "Data Category": "Nonpoint",
                "Last Updated Date": "3/14/2012",
                "SCC Level Two": "Industrial",
                "Tier 1 Code": "02",
                "SCC Level Three": "Waste oil"
            }
        },
        {
            "code": "2260001000",
            "attributes": {
                "Tier 3 Description": "Recreational",
                "Activity Value Required?": "No",
                "Sector": "Mobile - Non-Road Equipment - Gasoline",
                "SCC Level Four": "Total",
                "Tier 1 Description": "Off-Highway",
                "SCC Level One": "Mobile Sources",
                "Status": "Retired",
                "Tier 2 Description": "Non-Road Gasoline",
                "Data Category": "Nonroad",
                "Last Updated Date": "3/14/2012",
                "SCC Level Two": "Off-highway Vehicle Gasoline, 2-Stroke",
                "SCC Level Three": "Recreational Equipment",
                "Last Inventory Year": "2005"
            }
        },
        {
            "code": "2260002000",
            "attributes": {
                "Tier 3 Description": "Construction",
                "Status": "Active",
                "Activity Value Required?": "No",
                "Tier 2 Description": "Non-Road Gasoline",
                "Sector": "Mobile - Non-Road Equipment - Gasoline",
                "Data Category": "Nonroad",
                "Last Updated Date": "3/14/2012",
                "SCC Level Two": "Off-highway Vehicle Gasoline, 2-Stroke",
                "SCC Level Four": "Total",
                "Tier 1 Description": "Off-Highway",
                "SCC Level Three": "Construction and Mining Equipment",
                "SCC Level One": "Mobile Sources"
            }
        }
    ]
}

这是我控制器中的代码

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/json; charset=utf-8";

WebResponse response = request.GetResponse();

using (Stream responseStream = response.GetResponseStream())
{
    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

    string text = reader.ReadToEnd();

    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
    javaScriptSerializer.MaxJsonLength = Int32.MaxValue;
    List<EpaSccCodeModel> listEpaSccCodes  = (List<EpaSccCodeModel>)javaScriptSerializer.Deserialize(text, typeof(List<EpaSccCodeModel>));

这是我的EpaSccCodeModel

public class EpaSccCodeModel
{
    public string code { get; set; }

    public EpaAttributeModel AttributeModel { get; set; }

}

和属性模型

public class EpaAttributeModel
{
    [JsonProperty("Activity Value Required?")]
    public string ActivityValueRequired { get; set; }

    [JsonProperty("Data Category")]
    public string DataCategory { get; set; }

    [JsonProperty("ert valid")]
    public string ErtValid { get; set; }

    [JsonProperty("history")]
    public string History { get; set; }

    [JsonProperty("last inventory year")]
    public string LastInventoryYear { get; set; }

    [JsonProperty("last updated date")]
    public string LastUpdatedDate { get; set; }

    [JsonProperty("map to")]
    public string MapTo { get; set; }

    [JsonProperty("option group")]
    public string OptionGroup { get; set; }

    [JsonProperty("option set")]
    public string OptionSet { get; set; }

    [JsonProperty("scc level four")]
    public string SccLevelFour { get; set; }

    [JsonProperty("scc level one")]
    public string SccLevelOne { get; set; }

    [JsonProperty("scc level three")]
    public string SccLevelThree { get; set; }

    [JsonProperty("scc level two")]
    public string SccLevelTwo { get; set; }

    [JsonProperty("sector")]
    public string Sector { get; set; }

    [JsonProperty("short name")]
    public string ShortName { get; set; }

    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("tier 1 code")]
    public string Tier1Code { get; set; }

    [JsonProperty("tier 1 description")]
    public string Tier1Description { get; set; }

    [JsonProperty("tier 2 code")]
    public string Tier2Code { get; set; }

    [JsonProperty("tier 2 description")]
    public string Tier2Description { get; set; }

    [JsonProperty("tier 3 code")]
    public string Tier3Code { get; set; }

    [JsonProperty("tier 3 description")]
    public string Tier3Description { get; set; }

    [JsonProperty("usage notes")]
    public string UsageNotes { get; set; }
}

我得到了json数据,我可以看到代码字段,但属性模型为null。

有人能告诉我如何获取json的属性部分中的值吗?

1 个答案:

答案 0 :(得分:0)

你有一个如下所示的根对象类,

elements.length

现在,您可以在控制器中尝试反序列化RootObject,

public class RootObject
{
    [JsonProperty("Key")]
    public string key {get;set;}

    [JsonProperty("Value")]   
    public List<EpaSccCodeModel> Value {get;set;}
}

希望这有帮助!