Search code examples
asp.netjsonasp.net-mvcasp.net-web-apijson.net

How to fetch @value from following json


I have following json.I want to fetch "rdfs:label"'s value when "@type" is "T:Class".

I want output like Stand1 Stand2

How to achieved this.

I have tried like this.

string json = File.ReadAllText("Filepath"); JObject Search = JObject.Parse(json);

IList results = Search["@standard"][0].Children().ToList(); foreach (JToken result in results) { } It gives me entire inner @standard block.

{
  "@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"
          }
        }
]
}
]
}

Solution

  • try this

        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
    

    helper

    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);
                }
            }
        }
    }