Search code examples
c#asp.netjsonjson.net

How to get label value from following json.I tried but no proper output


I have tried but no proper response. I have following json.I want to fetch rdfs:label's value when type is @type": "owl:DbType and rdfs:class is given any class (GreenPlant/GreenPlantHistory)

Ex : If @type": "owl:DbType and class is GreenPlant,fetch rdfs:label's value that is 'Involved' & 'Present'

I have tried following query.But unable to add classname in that.How to do that.

Query : -

List<string> jp = v1.Where(i => (string)i["@type"] == "owl:DbType")
  .Select(i => (string)((JObject)i).Properties().First(x => x.Name == "rdfs:label").Value["@value"]).ToList();

{
  "@id": "Ap:Involved",
  "@type": "owl:DbType",
  "rdfs:class": {
    "@id": "Ap:GreenPlant"
  },
  "tag:std:label": {
    "@value": "Involved"
  },
  "rdfs:label": {
    "@value": "Involved"
  },
  "rdfs:range": {
    "@id": "xsd:boolean"
  }
},

{
  "@id": "Ap:Present",
  "@type": "owl:DbType",
  "rdfs:class": {
    "@id": "Ap:GreenPlant"
  },
  "tag:std:label": {
    "@value": "Present"
  },
  "rdfs:label": {
    "@value": "Present"
  },
  "rdfs:range": {
    "@id": "xsd:boolean"
  }
},

{
  "@id": "Ap:UserName",
  "@type": "owl:DbType",
  "rdfs:class": {
    "@id": "Ap:GreenPlantHistory"
  },
  "tag:std:label": {
    "@value": "UserName"
  },
  "rdfs:label": {
    "@value": "UserName"
  },
  "rdfs:range": {
    "@id": "xsd:string"
  }
},

{
  "@id": "Ap:Name",
  "@type": "owl:ObjType",
  "rdfs:class": {
    "@id": "Ap:GreenPlantHistory"
  },
  "tag:std:label": {
    "@value": "Name"
  },
  "rdfs:label": {
    "@value": "Name"
  },
  "rdfs:range": {
    "@id": "xsd:string"
  }
}

Solution

  • Your json is invalid, wrap it into [] and try this

    var jsonParsed = JArray.Parse(json);
    var found=FindValues(jsonParsed, "owl:DbType", "Ap:GreenPlant");
    

    test

    Console.WriteLine(string.Join(",", found)); //Involved,Present
    

    helper

    public List<string> FindValues(JArray jsonParsed, string strType , string strClass)
    {
    return jsonParsed.Where(i => (string)i["@type"] == strType 
           && (string)i["rdfs:class"]["@id"] == strClass)
        .Select(i => (string) i["rdfs:label"]["@value"]).ToList();
    }