仅反序列化JSON响应的单个节点 [英] Deserializing just a single node of a JSON response

查看:62
本文介绍了仅反序列化JSON响应的单个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON响应

{
  "appStatus":{
    "status":true
  },
  "lastSyncDate":"06-07-2013 13.54.27",
  "configResponse":{
    "status":{
      "status":true
    },
    "configs":{
      "APPLOGENABLED":{
        "configCode":"APPLOGENABLED",
        "configDesc":"enable or disable logging from front end",
        "configType":"TAB",
        "configValue":"Y"
      },
      "COMMENTSTIMER":{
        "configCode":"COMMENTSTIMER",
        "configDesc":"timer for comments in seconds",
        "configType":"TAB",
        "configValue":"60"
      },
      "SUMMARYTIMER":{
        "configCode":"SUMMARYTIMER",
        "configDesc":"timer for summary in seconds",
        "configType":"TAB",
        "configValue":"30"
      },
      "ALERTSTIMER":{
        "configCode":"ALERTSTIMER",
        "configDesc":"timer for alerts in seconds",
        "configType":"TAB",
        "configValue":"60"
      }
    },
    "lastSyncDate":"06/07/2013 13.48.13"
  }
}

我要使用json.NET将configResponse提取到字典中.我知道我可以像这样直接转换Dictionary对象

Using json.NET, I want to extract configResponse into dictionary. I know i can directly convert a Dictionary object like this

  JsonConvert.DeserializeObject<Dictionary<string,string>>().... 

但是由于configResponse是一个子元素,所以我无法根据需要进行解析.

but since configResponse is a sub element, I am not able to parse it as required.

我想将上述json响应反序列化为

I want to deserialize the above json response as

public class ConfigResponse
{
    public Status status { get; set; }
    public Dictionary<string, ConfigurationItem> configs { get; set; }
    public string lastSyncDate { get; set; }
}

public class ConfigurationItem
{
    public String configCode { get; set; }
    public String configDesc { get; set; }
    public String configType { get; set; }
    public String configValue { get; set; }
}

推荐答案

我只使用了这种方法,并且有效.

i have only use this method and it worked.

 await JsonConvert.DeserializeObjectAsync<ConfigResponse>(jsontoobject, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

这篇关于仅反序列化JSON响应的单个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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