0

我与以下 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;"
        }

以及如何检查小部件父节点是否包含带有 linq 的图像子节点?如果小部件父包含指定的子节点,我有 if-else 条件。

如果父级有子级属性,我将向我的数据库提供属性数据并在代码块返回 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..

但是当 Parent 没有指定子节点并运行 else 块时,我没有这样做.. Best Regards 。

4

1 回答 1

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;"
        }
    }
}
于 2014-06-06T15:53:26.553 回答