使用 Json.net C# 反序列化 json - 标识符中的空格
我的问题的一些背景:我正在寻找在 Json.net 中反序列化一些可怕的 json 。这个 json 对象由 ArcGIS Server 创建,并发送一个格式如下的“结果”对象:
{ "results" : [ { "layerId" : 10, "layerName" : "Polling Districts", "value" : "MyWard", "displayFieldName" : "Ward", "attributes" : { "OBJECTID" : "61", "Ward" : "MyWard", "Polling Station" : "Childrens Resources Centre", "Polling District" : "E", "Constituency" : "South", "Shape" : "Polygon" } } ] }
现在的问题是属性对象:
{ "OBJECTID" : "61", "Ward" : "MyWard", "Polling Station" : "Childrens Resources Centre", "Polling District" : "E", "Constituency" : "South", "Shape" : "Polygon" }
...在某些标识符中有空格;它是由“漂亮”字段别名生成的,而不是数据表名称。我可以使用 linq selectToken 获取所有其他字段,但我特别寻找“投票站”。
我已经尝试了很多查询变体:
string pollingStation = (string)jObj.SelectToken("results[0].attributes[Polling Station]"); // Unexpected character while parsing path indexer: P
string pollingStation = (string)jObj.SelectToken("results[0].attributes[\"Polling Station\"]"); //Unexpected character while parsing path indexer: "
string pollingStation = (string)jObj.SelectToken("results[0].attributes.\"Polling Station\""); // No error, pollingStation is null
string pollingStation = (string)jObj.SelectToken("results[0].attributes.Constituency"); // No error, pollingStation is South (correct)
我用谷歌搜索,搜索了 json.net 帮助&阅读这里已经发布的很多问题 - 其中没有一个似乎涉及这个特定问题。也许我只是太笨了。您可能还会看出我不精通 c# 或 Linq,而且这是我第一次使用 Json.net,所以我可能犯了一些小学生的错误。欢迎任何建议!
Some background to my question: I'm looking to deserialize some horrid json within Json.net. This json object is created by ArcGIS Server, and sends a 'results' object which is formatted thusly:
{ "results" : [ { "layerId" : 10, "layerName" : "Polling Districts", "value" : "MyWard", "displayFieldName" : "Ward", "attributes" : { "OBJECTID" : "61", "Ward" : "MyWard", "Polling Station" : "Childrens Resources Centre", "Polling District" : "E", "Constituency" : "South", "Shape" : "Polygon" } } ] }
Now the problem is the attributes object:
{ "OBJECTID" : "61", "Ward" : "MyWard", "Polling Station" : "Childrens Resources Centre", "Polling District" : "E", "Constituency" : "South", "Shape" : "Polygon" }
...which has whitespace in some identifiers; it's being generated by the 'pretty' field alias, and not the data table name. I'm able to use the linq selectToken to get all other fields, but I'm looking for "Polling Station" in particular.
I've tried quite a few variants of the query:
string pollingStation = (string)jObj.SelectToken("results[0].attributes[Polling Station]"); // Unexpected character while parsing path indexer: P
string pollingStation = (string)jObj.SelectToken("results[0].attributes[\"Polling Station\"]"); //Unexpected character while parsing path indexer: "
string pollingStation = (string)jObj.SelectToken("results[0].attributes.\"Polling Station\""); // No error, pollingStation is null
string pollingStation = (string)jObj.SelectToken("results[0].attributes.Constituency"); // No error, pollingStation is South (correct)
I've googled, searched the json.net help & read quite a few questions already posted here - none of which seem to deal with this particular question. Maybe I'm just being dense. You may also be able to tell I'm not proficient in c#, or Linq, and this is the first time I've used Json.net so I have probably made some schoolboy errors. Any suggestions welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读
JPath.cs
中的JPath.ParseMain
代码,有效的是备注: 在 2013 年之前的 Json.Net 中,它可以在没有任何形式的转义的情况下工作:
Reading the
JPath.ParseMain
code inJPath.cs
what works isRemark: in pre-2013 JSon.Net it worked without any form of escaping :