我有以下 json。當“@type”為“T:Class”時,我想獲取“rdfs:label”的值。
我想要像 Stand1 Stand2 這樣的輸出
如何實作這一點。
我試過這樣。
字串 json = File.ReadAllText("檔案路徑"); JObject 搜索 = JObject.Parse(json);
IList 結果 = Search["@standard"][0].Children().ToList(); foreach (JToken result in results) { } 它給了我整個內部 @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"
}
}
]
}
]
}
uj5u.com熱心網友回復:
試試這個
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);
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425441.html
標籤:网 json asp.net-mvc asp.net-web-api json.net
上一篇:遍歷非公開成員資料
