如何使用 JSON 路径获取 JSON 字符串的一部分而不是 JToken?

How to get section of JSON string instead of JToken using JSON path?

我正在寻找使用 SelectTokens(JPath) 获取部分 JSON 字符串而不是 JTOken 集合的最佳方法。

例如:

JObject o = JObject.Parse(@"{
      'Stores': [
        'Lambton Quay',
        'Willis Street'
      ],
      'Manufacturers': [
        {
          'Name': 'Acme Co',
          'Products': [
            {
              'Name': 'Anvil',
              'Price': 50
            }
          ]
        },
        {
          'Name': 'Contoso',
          'Products': [
            {
              'Name': 'Elbow Grease',
              'Price': 99.95
            },
            {
              'Name': 'Headlight Fluid',
              'Price': 4
            }
          ]
        }
      ]
    }");

List<JToken> manufactures = o.SelectTokens("Manufacturers");

我需要输出 JSON 字符串而不是 JToken 的集合。

预期输出:

{
    "Manufacturers": [
        {
            "Name": "Acme Co",
            "Products": [
                {
                    "Name": "Anvil",
                    "Price": 50
                }
            ]
        },
        {
            "Name": "Contoso",
            "Products": [
                {
                    "Name": "Elbow Grease",
                    "Price": 99.95
                },
                {
                    "Name": "Headlight Fluid",
                    "Price": 4
                }
            ]
        }
    ]
}

有没有办法得到这样的输出?

这里有两种方法: 您不能将其转换为您在提问时想要的精确 JSON ,但您可以将其提取为实际形式,即在这种情况下 - 一个数组。见下文:

Working Example in Fiddle

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
        public static JsonSerializer _serializer = new JsonSerializer();


    public static void Main()
    {
        JObject o = JObject.Parse(@"{
              'Stores': [
                'Lambton Quay',
                'Willis Street'
              ],
              'Manufacturers': [
                {
                  'Name': 'Acme Co',
                  'Products': [
                    {
                      'Name': 'Anvil',
                      'Price': 50
                    }
                  ]
                },
                {
                  'Name': 'Contoso',
                  'Products': [
                    {
                      'Name': 'Elbow Grease',
                      'Price': 99.95
                    },
                    {
                      'Name': 'Headlight Fluid',
                      'Price': 4
                    }
                  ]
                }
              ]
            }");

        Console.WriteLine("1. Print the key Value");
        Console.WriteLine(o["Manufacturers"].ToString());
        Console.WriteLine("--------");
        Console.WriteLine("2. Iterate and print by keyname - (Key - Value) ");

            foreach(var m in o){

                if(m.Key == "Manufacturers")
                Console.WriteLine(m.ToString());
            }


    }

}

另一种选择是您可以破解它 - 使用上面的示例提取字符串,然后将字符串添加到大括号之间

var json = "{" + extractedString + "}";