考虑此json:
{
"farms": [
{
"fred's": {
"cows": 5,
"pigs": 2,
"horses": "none"
}
}, {
"old mcdonald's": {
"cows": none,
"pigs": 1,
"horses": 3
}
}
]
}
单独使用单引号似乎没有问题:
JObject j = new JObject();
JToken jt = j.SelectToken(farms.fred's);
但是,尝试同时使用单引号和白色空间时会出现错误。
使用此代码:
JObject j = new JObject();
JToken jt = j.SelectToken(farms.old mcdonald's);
我有一个错误:
jsonexception:解析路径时出乎意料的字符
所以我进行了一些搜索,发现与白色空间的术语必须由以下方式包围:
[' ']
示例:
JObject j = new JObject();
JToken jt = j.SelectToken(farms.['old mcdonald's']);
但是,现在错误是:
jsonexception:解析路径索引时出乎意料的字符:s
是否可以在SelectToken路径中同时包含白色空间和单个引号?
似乎是...
var x = @"{
""farms"": [
{
""fred's"": {
""cows"": 5,
""pigs"": 2,
""horses"": ""none""
}
},
{
""old mcdonald's"": {
""cows"": ""none"",
""pigs"": 1,
""horses"": 3
}
}
]
}";
var j = JObject.Parse(x);
var jobj = j.SelectToken("farms[1]['old mcdonald\'s']") as JObject;
jobj.Properties().Select( p => p.Name + ": " + p.Value).Dump();
IEnumerable<String> (3 items)
* cows: none
* pigs: 1
* horses: 3