使用 Json.net C# 反序列化 json - 标识符中的空格

发布于 2024-11-25 09:39:51 字数 1485 浏览 2 评论 0原文

我的问题的一些背景:我正在寻找在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-12-02 09:39:51

阅读 JPath.cs 中的 JPath.ParseMain 代码,有效的是

var json = @"{ ""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"" } } ] }";
var jObj = JObject.Parse(json);
var pollingStation = (string)jObj.SelectToken("results[0].attributes.['Polling Station']");

备注: 在 2013 年之前的 Json.Net 中,它可以在没有任何形式的转义的情况下工作:

var pollingStation = (string)jObj.SelectToken("results[0].attributes.Polling Station");

Reading the JPath.ParseMain code in JPath.cs what works is

var json = @"{ ""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"" } } ] }";
var jObj = JObject.Parse(json);
var pollingStation = (string)jObj.SelectToken("results[0].attributes.['Polling Station']");

Remark: in pre-2013 JSon.Net it worked without any form of escaping :

var pollingStation = (string)jObj.SelectToken("results[0].attributes.Polling Station");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文