简体   繁体   English

将 JSON 序列化为动态时属性名称中的非法字符

[英]Illegal character in property name when serializing JSON to dynamic

I am trying to deserialize JSON that contains dash (-) characters in some of its property names, by using dynamic types:我正在尝试使用动态类型反序列化在其某些属性名称中包含破折号 (-) 字符的 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];

I cannot get the "container-title" value due to use of an illegal character.由于使用了非法字符,我无法获得“容器标题”值。 And I don't want to simply use Replace() to remove dash characters.而且我不想简单地使用Replace()来删除破折号字符。 Is there any way?有什么办法吗?

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

result.message["container-title"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM