使用Newtonsoft.Json.Linq.JToken.SelectToken解析JSON

时间:2017-09-07 04:45:35

标签: c# json json.net jsonpath

我正在尝试解析JSON以获取以下代码段中的VIN编号的值。 SelectToken调用返回null值。 我尝试使用相同的令牌在以下网站上进行测试,并在那里工作

我无法确定这里发生了什么。是因为Newtownsoft.Json不支持此令牌,还是其他

    [Test]
    public void Test()
    {
        string responseContent =
            "{\"GetAdvisorWipDetailPageResponse\":{\"AdvisorDetailPage\":{\"TaxLabel\":\"VAT\",\"Currency\":{\"Code\":\"UKL\",\"Description\":\"Sterling\",\"Symbol\":\"£\",\"Precision\":\"2\"},\"LastMileage\":\"0\",\"NetAmount\":\"52.73\",\"VATAmount\":\"10.55\",\"TotalAmount\":\"63.28\",\"VATRate\":\"20.00\",\"RTSLabourRate\":\"55.50\",\"DateOut\":\"2017-09-06\",\"TimeOut\":\"16:00\",\"CustomerName\":\"S 4133166Portor\",\"CustomerDetails\":{\"Name\":\"S 4133166Portor\",\"Address\":\"1 Alvin Street\\\\nHungerford\\\\n\\\\n\\\\n\"},\"ListSection\":[{\"Type\":\"2\",\"SectionDesc\":\"Vehicle Details\",\"Cols\":\"2\",\"DisplayRows\":\"10\",\"Rows\":\"10\",\"ListRow\":[{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Registration:\",\"Col2\":\"PRA4133166\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"VIN:\",\"Col2\":\"4133166\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Make:\",\"Col2\":\"Citroen\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Model:\",\"Col2\":\"C3 Picasso\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Colour:\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Fuel Type:\",\"Col2\":\"Petrol\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Last known odometer:\",\"Col2\":\"0\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Next service due:\",\"Col2\":\"1200 or 06/07/2018\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Registration date:\",\"Col2\":\"06/09/2017\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"MOT due date:\",\"Col2\":\"06/07/2018\"}}],\"Variant\":{\"Class\":\"CAR\",\"Desc\":\"Car\",\"Variants\":{\"Variant\":[{\"Class\":\"BIKE\",\"Desc\":\"Motorcycle\"},{\"Class\":\"CAR\",\"Desc\":\"Car\"},{\"Class\":\"COMP\",\"Desc\":\"Component vehicle\"},{\"Class\":\"HGV\",\"Desc\":\"Heavy Goods Vehicle\"},{\"Class\":\"LCV\",\"Desc\":\"Light Commercial Vehicle\"}]}}},{\"Type\":\"2\",\"SectionDesc\":\"Today's Service Details\",\"Cols\":\"2\",\"DisplayRows\":\"6\",\"Rows\":\"6\",\"ListRow\":[{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Repair number:\",\"Col2\":\"20285\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Account:\",\"Col2\":\"C0002 - Service Retail Cash Sales\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Notes:\",\"Col2\":\"Carry out repair :-\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Goods value:\",\"Col2\":\"£52.73\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"VAT value:\",\"Col2\":\"£10.55\"}},{\"WipNo\":\"20285\",\"Row\":{\"Col1\":\"Total value:\",\"Col2\":\"£63.28\"}}]},{\"Type\":\"4\",\"SectionDesc\":\"Recent Service History\"}],\"VHCCompleted\":\"0\",\"Reminders\":\"0\",\"PreviousVHC\":{\"Available\":\"0\"}},\"Version\":\"3.228\",\"Partition\":\"972\",\"Startup\":\"2017-09-06T15:40\",\"RequestsServiced\":\"1\"}}";
        string key = "GetAdvisorWipDetailPageResponse.AdvisorDetailPage.ListSection[0].ListRow[*].[?(@.Col1=='VIN:')].Col2";
        var content = JObject.Parse(responseContent);
        var value = content.SelectToken(key).ToString();
    }

未转义的字符串在这里

{"GetAdvisorWipDetailPageResponse":{"AdvisorDetailPage":{"TaxLabel":"VAT","Currency":{"Code":"UKL","Description":"Sterling","Symbol":"£","Precision":"2"},"LastMileage":"0","NetAmount":"52.73","VATAmount":"10.55","TotalAmount":"63.28","VATRate":"20.00","RTSLabourRate":"55.50","DateOut":"2017-09-06","TimeOut":"16:00","CustomerName":"S 4133166Portor","CustomerDetails":{"Name":"S 4133166Portor","Address":"1 Alvin Street\\nHungerford\\n\\n\\n"},"ListSection":[{"Type":"2","SectionDesc":"Vehicle Details","Cols":"2","DisplayRows":"10","Rows":"10","ListRow":[{"WipNo":"20285","Row":{"Col1":"Registration:","Col2":"PRA4133166"}},{"WipNo":"20285","Row":{"Col1":"VIN:","Col2":"4133166"}},{"WipNo":"20285","Row":{"Col1":"Make:","Col2":"Citroen"}},{"WipNo":"20285","Row":{"Col1":"Model:","Col2":"C3 Picasso"}},{"WipNo":"20285","Row":{"Col1":"Colour:"}},{"WipNo":"20285","Row":{"Col1":"Fuel Type:","Col2":"Petrol"}},{"WipNo":"20285","Row":{"Col1":"Last known odometer:","Col2":"0"}},{"WipNo":"20285","Row":{"Col1":"Next service due:","Col2":"1200 or 06/07/2018"}},{"WipNo":"20285","Row":{"Col1":"Registration date:","Col2":"06/09/2017"}},{"WipNo":"20285","Row":{"Col1":"MOT due date:","Col2":"06/07/2018"}}],"Variant":{"Class":"CAR","Desc":"Car","Variants":{"Variant":[{"Class":"BIKE","Desc":"Motorcycle"},{"Class":"CAR","Desc":"Car"},{"Class":"COMP","Desc":"Component vehicle"},{"Class":"HGV","Desc":"Heavy Goods Vehicle"},{"Class":"LCV","Desc":"Light Commercial Vehicle"}]}}},{"Type":"2","SectionDesc":"Today's Service Details","Cols":"2","DisplayRows":"6","Rows":"6","ListRow":[{"WipNo":"20285","Row":{"Col1":"Repair number:","Col2":"20285"}},{"WipNo":"20285","Row":{"Col1":"Account:","Col2":"C0002 - Service Retail Cash Sales"}},{"WipNo":"20285","Row":{"Col1":"Notes:","Col2":"Carry out repair :-"}},{"WipNo":"20285","Row":{"Col1":"Goods value:","Col2":"£52.73"}},{"WipNo":"20285","Row":{"Col1":"VAT value:","Col2":"£10.55"}},{"WipNo":"20285","Row":{"Col1":"Total value:","Col2":"£63.28"}}]},{"Type":"4","SectionDesc":"Recent Service History"}],"VHCCompleted":"0","Reminders":"0","PreviousVHC":{"Available":"0"}},"Version":"3.228","Partition":"972","Startup":"2017-09-06T15:40","RequestsServiced":"1"}}

1 个答案:

答案 0 :(得分:1)

以下JSONPath查询字符串适用于Json.NET:

string key = "GetAdvisorWipDetailPageResponse.AdvisorDetailPage.ListSection[0].ListRow[?(@.Row.Col1=='VIN:')].Row.Col2";

然后返回4133166

工作.Net fiddle

为什么这样做?如果我简化JSON的ListRow部分,它看起来像这样:

{
   "ListRow":[
      {
         "WipNo":"20285",
         "Row":{
            "Col1":"Registration:",
            "Col2":"PRA4133166"
         }
      },
      {
         "WipNo":"20285",
         "Row":{
            "Col1":"VIN:",
            "Col2":"4133166"
         }
      }
   ]
}

您正在ListRow数组中查找Row.Col1具有特定值的条目,具体为"VIN:"。找到后,您需要选择Row.Col2的值。 JSONPath查询字符串

ListRow[?(@.Row.Col1=='VIN:')].Row.Col2

这样做。

(在您的查询中,您尝试将过滤器应用于直接嵌套在对象内的对象(具体而言,在Col数组项内的Row对象内过滤ListRow的值从10.0.3开始,这显然没有在Json.NET中实现;请参阅Json.NET JSONPath query not returning expected results进一步讨论。)