如何从json响应中获取和附加输出数据,并使用RobotFramework和python将重复的密钥(如果有)存储在文件中?
我有一个带有嵌套值的json响应。我已经从json中读取了所需的键和值。我面临的问题是,我无法在存储的json文件中附加特定的键和值。那么,谁能帮我解决这个问题呢 JSON响应具有多个键和值的标记,如:如何从json响应中获取和附加输出数据,并使用RobotFramework和python将重复的密钥(如果有)存储在文件中?,python,json,robotframework,Python,Json,Robotframework,我有一个带有嵌套值的json响应。我已经从json中读取了所需的键和值。我面临的问题是,我无法在存储的json文件中附加特定的键和值。那么,谁能帮我解决这个问题呢 JSON响应具有多个键和值的标记,如: ${API_Output}= { "data": { "resources": { "edges": [ { "node": { "tags": [], } }, { "node": {
${API_Output}= {
"data": {
"resources": {
"edges": [
{
"node": {
"tags": [],
}
},
{
"node": {
"tags": [
{
"name": "app",
"value": "e2e"
},
{
"name": "Cost",
"value": "qwerty"
}
}
},
{
"node": {
"tags": [
{
"name": "app",
"value": "e2e"
},
{
"name": "Cost",
"value": "qwerty"
},
{
"name": "test",
"value": "qwerty"
}
}
}
]
}
}
}
机器人框架代码:
${dict1}= Set Variable ${API_Output}
${cnt}= get length ${dict1['data']['resources']['edges']}
${edge}= set variable ${dict1['data']['resources']['edges']}
run keyword if ${cnt}==0 set test message The resources count is Zero(0)
log to console ${cnt}-count
: FOR ${item} IN RANGE 0 ${cnt}
\ ${readName}= Set Variable ${edge[${item}]['node']['configuration']}
\ ${readvalue2}= Set Variable ${edge[${item}]['node']['tags']}
\ ${tag_Count}= get length ${edge[${item}]['node']['tags']}
\ ${tag_variable}= set variable ${edge[${item}]['node']['tags']}
\ forkeyword ${tag_Count} ${tag_variable} ${readName}
${req_json} Json.Dumps ${dict}
Create File results.json ${req_json}
forkeyword
[Arguments] ${tag_Count} ${tag_variable} ${readName}
@{z}= create list
: FOR ${item} IN RANGE 0 ${tag_Count}
\ ${resourceName}= run keyword if ${tag_Count} > 0 set variable ${readName['name']}
\ log to console ${resourceName}-forloop
\ ${readkey}= set variable ${tag_variable[${item}]['name']}
\ ${readvalue}= set variable ${tag_variable[${item}]['value']}
\ set to dictionary ${dict} resourceName ${resourceName}
\ set to dictionary ${dict} ${readkey} ${readvalue}
set suite variable ${dict}
我期望所有标签中的所有值,但只打印最后一个标签键和值。谁能告诉我密码吗
提前感谢您的帮助 首先为什么会有重复的密钥 JSON中的重复键不在规范中,可能导致未定义的行为。如果将JSON读入Python dict,信息将丢失,因为Python dict键必须是唯一的。您最好的选择是更改JSON;复制钥匙是个坏主意 另外,如果您将json更改为没有重复项,它将给出正确的结果 因为python键必须是唯一的 由于所有其他键都是重复的,您的简化版本将是:
"data": {
"resources": {
"edges": [
{
"node": {
"tags": [
{
"name": "app",
"value": "e2e"
},
{
"name": "Cost",
"value": "qwert"
},
{
"name": "test",
"value": "qwerty"
}
]
}
}
]
}
}
所以如果你的机器人框架给出的结果是
{
"name": "app",
"value": "e2e"
},
{
"name": "Cost",
"value": "qwert"
},
{
"name": "test",
"value": "qwerty"
}
这是正确的。@robotframework,有人能对此提供指导吗code@robotframework用户,我被困在这段代码中,你能帮我解决这个问题吗。我只需要获取所有标记、键和值,并将其存储在json文件中。如果我不清楚,很抱歉!。我需要获取所有的标签响应。如果在响应中看到,则有两个标签响应。所以我需要获取所有文件并将它们附加到一个文件中,我需要代码方面的帮助。明白!如果有dict不支持重复密钥。是否有其他方法获取和存储它们。json中应该避免重复,但这可能会有所帮助