如何从关注 json 中获取@value

How to fetch @value from following json

我有以下 json.I 想要在“@type”为“T:Class”时获取“rdfs:label”的值。

我想要这样的输出 展位1 Stand2

如何做到这一点。

我试过这样。

字符串json = File.ReadAllText("文件路径"); JObject 搜索 = JObject.Parse(json);

IList results = Search["@standard"][0].Children().ToList(); foreach(JToken 结果中的结果) { } 它给了我整个内部@standard 块。

{
  "@School": {
    "name": "Vikas Mandir",
    "subject": "Maths",
    "author": "Joshi",
  
  },
  "@standard": [
    {
      "@id": "vikas:Stand1",

      "@standard": [
        
        {
          "@id": "vikas:Stand12",
          "@type": "T:Class",
          "tag:Std:label": {
            "@value": "Stand1"
          },
          "rdfs:label": {
            "@value": "Stand1"
          }
        },

        {
          "@id": "vikas:Stand123",
          "@type": "T:Class",
          "tag:Std:label": {
            "@value": "Stand2"
          },
          "rdfs:label": {
            "@value": "Stand2"
          }
        }
]
}
]
}

试试这个

    var prop = new List<JToken>();

    GetObject(jsonParsed, prop, "rdfs:label", null, true);
    List<string> found = prop.Where(i => (string)i["@type"] == "T:Class")
    .Select(i => (string) ((JObject)i).Properties().First(x => x.Name == "rdfs:label").Value["@value"]).ToList();
    
    Console.WriteLine(string.Join(",",found)); //Stand1,Stand2

帮手

public void GetObject(JToken obj, List<JToken> result, string name = null, string value = null, bool getParent = false)
{
    if (obj.GetType().Name == "JObject")
        foreach (var property in ((JObject)obj).Properties())
        {
            if (property.Value.GetType().Name == "JValue"
                && (name == null || (string)property.Name == name)
                && (value == null || (string)property.Value == value))
            {
                if (getParent) result.Add(property.Parent); else result.Add(property);
            }
            else if (property.Value.GetType().Name == "JObject")
            {
                if (name != null && (string)property.Name == name) result.Add(property.Parent);
                GetObject(property.Value, result, name, value);
            }
            else if (property.Value.GetType().Name == "JArray") GetObject(property.Value, result, name, value);
        }
    if (obj.GetType().Name == "JArray")
    {
        foreach (var property in ((JArray)obj))
        {
            if (property.GetType().Name == "JObject") GetObject(property, result, name, value);
            if (property.GetType().Name == "JArray") GetObject(property, result, name, value);
            if (property.GetType().Name == "JValue"
                 && (value == null || (string)property == value))
            {
                if (getParent) result.Add((JArray)obj); else result.Add(property);
            }
        }
    }
}