net如何将jsonpath与

net如何将jsonpath与

本文介绍了Json.net如何将jsonpath与"$"一起使用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试从json使用json.net,如下所示:

I'm trying to use json.net from json as follows:

String JSONString =
@"[
    {
      ""category"": ""reference"",
      ""author"": ""Nigel Rees"",
      ""title"": ""Sayings of the Century"",
      ""price"": 8.95
    },
    {
      ""category"": ""fiction"",
      ""author"": ""Still Here"",
      ""title"": ""Test remove title"",
      ""price"": 12.99,
      ""isbn"": ""0-553-21311-3""
    }
  ]";

JObject JSONObject;
JSONObject = JObject.Parse(JSONString);

String JSONPath = @"$[0].title";
JSONObject.SelectToken(JSONPath);

获取异常:

ST.Acxiom.Test.DataJSONTest.DataJSONClass.GetToken: Newtonsoft.Json.JsonException :   Property '$' does not exist on JObject.

  • 我做错了什么,即使我使用的是有效的jsonpath,但仍然遇到错误.
  • 是"$".不支持?
  • 如何访问中的数组项上面的示例中是json吗?
    • What I'm doing wrong, even though I'm using valid jsonpath but stillgetting error.
    • Is "$." not supported?
    • How to access Array item injson in above example?
    • 任何帮助将不胜感激.

      Any help would be appreciated.

      推荐答案

      1. 在示例中使用JObject.Parse会在最新的Json.net版本中抛出JsonReaderException.您必须使用JToken.ParseJsonConvert.DeserializeObject.
      2. SelectToken 用于选择子节点,因此不支持.您可以像这样访问数组项:
      1. Using JObject.Parse from your example throws JsonReaderException with the latest Json.net version. You have to use either JToken.Parse or JsonConvert.DeserializeObject.
      2. SelectToken is intended to select child nodes so $ is not supported. You can access array item like this:
      var jArray = JToken.Parse(JSONString); //It's actually a JArray, not a JObject
      var jTitle = jArray.SelectToken("[0].title");
      var title = (string)jTitle;
      

      这篇关于Json.net如何将jsonpath与"$"一起使用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:15