我跟随Json数据一样
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
我如何检查widget父节点包含带linq的图像子节点?如果widget父级包含指定的子节点,我有if-else条件。
如果Parent有子属性,我将为我的db提供属性数据,如果代码块返回true则运行。
我尝试过查询哪些子节点与父节点匹配。
if(!((from x in widget[i].Children() where x.Contains("image") select x) is Nullable))
something else..
else
something else..
如果父母没有任何指定的孩子,我给出了一个带有子值的参数,运行else块状态并返回false。
我尝试过查询哪些子节点与父节点不匹配。
if(!((from x in widget[i].Children() where x.Contains("link") select x) is Nullable))
something else..
else
something else..
但是,当父母没有指定子节点并运行其他阻止时,我没有做到。 最诚挚的问候。
答案 0 :(得分:1)
这里不需要查询 - 只需按键访问令牌:
JObject obj = JObject.Parse(json);
bool imageExists = obj["widget"]["image"] != null;
假设你有以下JSON:
{
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}