【问题标题】:Illegal character in property name when serializing JSON to dynamic将 JSON 序列化为动态时属性名称中的非法字符
【发布时间】:2016-04-19 18:05:06
【问题描述】:

我正在尝试通过使用动态类型来反序列化在其某些属性名称中包含破折号 (-) 字符的 JSON:

string json = MyWebClient.DownloadString("http://api.crossref.org/works/10.1093/brain/75.2.187");
dynamic result = JsonConvert.DeserializeObject<dynamic>(json);
string title = result.message.title[0];
string journal = result.message.container-title[0];

由于使用了非法字符,我无法获取“container-title”值。而且我不想简单地使用Replace() 来删除破折号。有什么办法吗?

【问题讨论】:

    标签: c# dynamic json.net


    【解决方案1】:

    由于message 也是JObject,您可以像字典一样访问它的属性

    result.message["container-title"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多