首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JSON.Net反序列化JSON.Net

用JSON.Net反序列化JSON.Net
EN

Stack Overflow用户
提问于 2014-02-19 21:33:58
回答 1查看 2.3K关注 0票数 2

我想使用JSON.Net反序列化这个模式。

代码语言:javascript
复制
{ 
  "color" : { 
    "type" : "String", 
    "description" : "What color do you want your taco", 
    "required" : false, 
    "default" : "Green", 
    "options" : [ "Green", "Blue", "Red"]
  },
  "include_beans" : { 
    "type"  : "Boolean", 
    "description" : "Do you want beans on your taco", 
    "required" : false, 
    "default" : false 
  }, 
  "pounds"  : { 
    "type" : "Double", 
    "description" : "How  many pounds of meat do you want?", 
    "required"  : false, 
    "default" : 0.1 
  },  
  "count" : { 
    "type"  : "Integer", 
    "description" : "How  many tacos would you like?", 
    "required"  : false, 
    "default" : 0.0 
  }
}

注意,每个属性都具有相同的结构。我想得到的是一个Dictionary<string, TacoProperty>,其中TacoProperty被定义为:

代码语言:javascript
复制
public class TacoProperty
{
    public string type { get; set; }
    public string description { get; set; }
    public bool required { get; set; }
    [JsonProperty(PropertyName = "default")]
    public string defaultValue { get; set; }
    public List<string> options { get; set; }
}

字典中的键应该是“颜色”、"include_beans“等等,所有的TacoProperty都应该是值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-19 21:39:02

Json.NET可以直接反序列化数据:

代码语言:javascript
复制
var tacoProperties =
       JsonConvert.DeserializeObject<IDictionary<string, TacoProperty>>(json);
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21892942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档