Illegal character in property name when serializing JSON to dynamic

一世执手 提交于 2020-04-13 06:35:13

问题


I am trying to deserialize JSON that contains dash (-) characters in some of its property names, by using dynamic types:

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. Is there any way?


回答1:


Since message is also a JObject you can access its properties like a dictionary

result.message["container-title"]


来源:https://stackoverflow.com/questions/36726153/illegal-character-in-property-name-when-serializing-json-to-dynamic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!