如何从下面的json中获取@value



我有下面的json。我想把";rdfs:标签"'s值时@类型";是";T: 类";。

我想要像这样的输出支架1支架2

如何做到这一点。

我试过这样。

string json=File.ReadAllText("文件路径"(;JObject Search=JObject.Parse(json(;

IList results=搜索[quot;@standard"][0].Children((.ToList((;foreach(结果中的JToken结果({}它给了我整个内在的标准块。

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

相关内容

  • 没有找到相关文章

最新更新