从 JSON 对象创建字典并检索键和值

Creating Dictionary from JSON Object and retreive key and value

我有一个 JSON 字符串如下:

{
    "UAT": {
        "single account creation": {
            "form validator": {
                "selector": {
                    "anchor": {},
                    "nav": {},
                    "element": {
                        "html": {
                            "title": "Create Single User Account"
                        },
                        "webctrl": {
                            "id": "licenseid-inputEl",
                            "aaname": "License:",
                            "tag": "INPUT"
                        }
                    }
                },
                "javascript": {}
            },
            "license": {
                "selector": {
                    "dropdown": {
                        "button": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "id": "ext-gen1075",
                                    "aria-role": "button",
                                    "tag": "DIV"
                                }
                            }
                        },
                        "menu": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "id": "boundlist-1039-listEl",
                                    "tag": "DIV"
                                }
                            }
                        },
                        "item": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "tag": "LI",
                                    "parentid": "boundlist-1039-listEl",
                                    "aaname": "{0}"
                                }
                            }
                        }
                    }
                },
                "javascript": {}
            },
            "city": {
                "selector": {
                    "textbox": {
                        "textbox": {
                            "anchor": {
                                "webctrl": {
                                    "id": "textfield-1029-labelEl",
                                    "tag": "LABEL",
                                    "aaname": "City:"
                                }
                            },
                            "nav": {
                                "up": "2"
                            },
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "tag": "INPUT",
                                    "type": "text",
                                    "aaname": "City:",
                                    "id": "textfield-1029-inputEl"
                                }
                            }
                        }
                    }
                },
                "javascript": {}
            },
            "country": {
                "selector": {
                    "dropdown": {
                        "button": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "id": "ext-gen1083",
                                    "aria-role": "button",
                                    "tag": "DIV"
                                }
                            }
                        },
                        "menu": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "id": "boundlist-1039-listEl",
                                    "tag": "DIV"
                                }
                            }
                        },
                        "item": {
                            "anchor": {
                                "webctrl": {
                                    "id": "countryCombo-labelEl",
                                    "tag": "LABEL",
                                    "aaname": "Country:"
                                }
                            },
                            "nav": {
                                "up": "8"
                            },
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "tag": "LI",
                                    "aaname": "{0}"
                                }
                            }
                        }
                    }
                },
                "javascript": {}
            },
            "region": {
                "selector": {
                    "dropdown": {
                        "button": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "id": "ext-gen1083",
                                    "aria-role": "button",
                                    "tag": "DIV"
                                }
                            }
                        },
                        "menu": {
                            "anchor": {},
                            "nav": {},
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "id": "boundlist-1039-listEl",
                                    "tag": "DIV"
                                }
                            }
                        },
                        "item": {
                            "anchor": {
                                "webctrl": {
                                    "id": "countryCombo-labelEl",
                                    "tag": "LABEL",
                                    "aaname": "Country:"
                                }
                            },
                            "nav": {
                                "up": "8"
                            },
                            "element": {
                                "html": {
                                    "title": "Create Single User Account"
                                },
                                "webctrl": {
                                    "tag": "LI",
                                    "aaname": "{0}"
                                }
                            }
                        }
                    }
                },
                "javascript": {}
            }
        }
    }
}

这里,国家城市执照表单验证器 等是具有两个 属性 的元素的名称,即 selectorjavascript。现在我想遍历这些元素中的任何一个并将属性的值作为字符串获取。

例如:如果我想获取 city->textbox->textbox 的字符串,输出将如下所示:

<html title='Create Single User Account'/>
<webctrl id='textfield-1029-labelEl' tag='LABEL' aaname:'City:'/>
<nav up='2'/>
<webctrl tag='INPUT' type='text' aaname='City:' id='textfield-1029-inputEl' />

同样,如果我想获取 license->dropdown->button 的字符串,输出将如下所示:

<html title='Create Single User Account'/>
<webctrl id='ext-gen1075' aria-role='button' tag='DIV' />

这背后的逻辑是:

  1. 第一行将包含 html 遍历路径(city->textbox- >文本框或许可证->下拉->按钮)
  2. 然后 anchor 值将就位,如果 anchor 值为空,它将跳过它。
  3. 然后 nav 也是如此,如果存在该值,则将其放置在 anchor 之后,否则将被跳过。
  4. 最后一行将包含 元素 内的 webctrl 值。该值将始终存在。

我已经使用 Newtonsoft.Linq.JSON 反序列化了 JSON 字符串并获得了一个 JObject 变量。我已经遍历了 city->textbox->textbox 的路径 as

JObject("UAT")("single account creation")("city")("selector")("textbox")("textbox")

或许可证->下拉->按钮为

JObject("UAT")("single account creation")("license")("selector")("dropdown")("button") 

并收到一个 Jtoken 变量,比如 myJToken。现在我无法进一步推断属性并获取要连接的值并创建我想要的字符串。谁能帮我解决这个问题。我正在使用 UiPath 来执行此操作,因此没有创建自定义 class 和反序列化 JSON 字符串并解析它的选项。

为了更好地理解 JSON 字符串的键是 斜体 而我正在寻找的自定义元素是 粗体。

因此 JObject("UAT")("single account creation")("city")("selector")("textbox")("textbox") 将 return 将您要解析为字符串的以下内容用作 UiPath activity 的选择器。所以下面的JSON会变成:

<html title='Create Single User Account'/>
<webctrl id='textfield-1029-labelEl' tag='LABEL' aaname:'City:'/>
<nav up='2'/>
<webctrl tag='INPUT' type='text' aaname='City:' id='textfield-1029-inputEl' />
{
  "anchor": {
    "webctrl": {
      "id": "textfield-1029-labelEl",
      "tag": "LABEL",
      "aaname": "City:"
    }
  },
  "nav": {
    "up": "2"
  },
  "element": {
    "html": {
      "title": "Create Single User Account"
    },
    "webctrl": {
      "tag": "INPUT",
      "type": "text",
      "aaname": "City:",
      "id": "textfield-1029-inputEl"
    }
  }
}

因此,为了构建第一个选择器,我有以下内容:

3 次在 UiPath 中分配

strJSONString = your JSON string

JOb = JObject.PArse(strJSONString)

strSelector = "<html title='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("element")("html")("title").ToString + "'/>"

后跟 2 个带有赋值的 if 语句,以说明 2 个可选部分 anchornav

if JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("anchor").Children.Count > 0

strSelector = strSelector + "<webctrl id='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("anchor")("webctrl")("id").ToString + "' tag='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("anchor")("webctrl")("aaname").ToString + ":/>"

if JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("nav").Children.Count > 0

strSelector = strSelector + "<nav up='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("nav")("up").ToString + "'/>"

最后是最后一个 webctrl 部分的分配

strSelector = strSelector + "<webctrl tag='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("element")("webctrl")("tag").ToString + "' type='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("element")("webctrl")("type").ToString + "' aaname='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("element")("webctrl")("aaname").ToString + ":' id='" + JOb("UAT")("single account creation")("city")("selector")("textbox")("textbox")("element")("webctrl")("id").ToString + "' />"

这给出了以下选择器

<html title='Create Single User Account'/>
<webctrl id='textfield-1029-labelEl' tag='City::'/>
<nav up='2'/>
<webctrl tag='INPUT' type='text' aaname='City::' id='textfield-1029-inputEl' />