2015-10-19 156 views
0

我正在测试使用机器人框架和请求库的REst API API的JSON响应采用表格形式。 我正在使用机器人框架来测试这个API,我想将这个表分成字典,以便我可以使用机器人框架关键字来测试响应。如何从JSON表中提取字典

这里是我的JSON响应的例子:

[ 
    { 
     locale: "fr_FR", 
     day: "2015-12-01", 
     displayPricePerPerson: 9800, 
     displayTotalPrice: 9800, 
     promotion: false, 
     minLos: 1 
}, 
{ 
     locale: "fr_FR", 
     day: "2015-12-02", 
     displayPricePerPerson: 9800, 
     displayTotalPrice: 9800, 
     promotion: false, 
     minLos: 1 
}, 
[ 

理想我想提取词典的形式整体响应,这样我可以在响应循环,并断言键和值。

所以不是嵌入到一个表dictionnaries的我就只有字典:

{ 
     locale: "fr_FR", 
     day: "2015-12-01", 
     displayPricePerPerson: 9800, 
     displayTotalPrice: 9800, 
     promotion: false, 
     minLos: 1 
}, 
{ 
     locale: "fr_FR", 
     day: "2015-12-02", 
     displayPricePerPerson: 9800, 
     displayTotalPrice: 9800, 
     promotion: false, 
     minLos: 1 
}, 

我试图与收藏,并请求库,但我得到了一个错误:

${JSON}= To JSON ${resp.content} 
${DICT}= Convert To List ${JSON} 
Log ${DICT} 
:FOR ${KEY} IN locale day displayPricePerPerson displayTotalPrice promotion minLos 
      \ Run Keyword And Continue On Failure List Should Contain Value ${JSON} ${KEY} 

Error : ValueError: dictionary update sequence element #0 has length 6; 2 is required 
+0

'convert to list'将数据转换为列表,而不是字典。你为什么认为它会返回一本字典? –

回答

1
   you have dictionaries in your list already. though badly formed.they are missing quotes around the keys 

        lt = [ 
         { 
          "locale": "fr_FR", 
          "day": "2015-12-01", 
          "displayPricePerPerson": 9800, 
          "displayTotalPrice": 9800, 
          "promotion": False, 
          "minLos": 1 
        }, 
        { 
          "locale": "fr_FR", 
          "day": "2015-12-02", 
          "displayPricePerPerson": 9800, 
          "displayTotalPrice": 9800, 
          "promotion": False, 
          "minLos": 1 
        }, 
        ] 

        for el in lt: 
         print(type(el)) 

        <class 'dict'> 
        <class 'dict'> 

        d1 = lt[0] 
        d2 =lt[1] 

        print(d1.items()) 
        dict_items([('day', '2015-12-01'), ('locale', 'fr_FR'), ('displayPricePerPerson', 9800), ('minLos', 1), ('promotion', False), ('displayTotalPrice', 9800)]) 

        print(d2.items()) 
        dict_items([('day', '2015-12-02'), ('locale', 'fr_FR'), ('displayPricePerPerson', 9800), ('minLos', 1), ('promotion', False), ('displayTotalPrice', 9800)]) 

       to convert to list of dicts this: 
lt2 = '''[ 
    { 
     locale: "fr_FR", 
     day: "2015-12-01", 
     displayPricePerPerson: 9800, 
     displayTotalPrice: 9800, 
     promotion: false, 
     minLos: 1 
    }, 
    { 
     locale: "fr_FR", 
     day: "2015-12-02", 
     displayPricePerPerson: 9800, 
     displayTotalPrice: 9800, 
     promotion: false, 
     minLos: 1 
    }, 
] 
''' 

    def convert_ECMA_Javascript(st): 
     # convert badly formed strings to json format 
     import json 

     result = re.sub(r'(\w+):',r'"\1":',st) 
     result= re.sub(r'false',r'"False"',result) 
     result= re.sub(r'true',r'"True"',result) 
     result= re.sub(r'\[|\]',r'',result) 
     result= re.sub(r'\b(\d+)\b(?!"|-)',r'"\1"',result) 
     result= re.sub(r'\n|\t',r'',result) 
     li = re.findall(r'{.*?}', result) 

     result = [] 
     for s in li: 
      result.append(json.loads(s)) 

     return result 

    pp(convert_ECMA_Javascript(lt2)) 

    [{'day': '2015-12-01', 
     'displayPricePerPerson': '9800', 
     'displayTotalPrice': '9800', 
     'locale': 'fr_FR', 
     'minLos': '1', 
     'promotion': 'False'}, 
    {'day': '2015-12-02', 
     'displayPricePerPerson': '9800', 
     'displayTotalPrice': '9800', 
     'locale': 'fr_FR', 
     'minLos': '1', 
     'promotion': 'False'}] 

    for el in convert_ECMA_Javascript(lt2): 
     for k,v in el.items(): 
      print(k,":", v) 

    day : 2015-12-01 
    displayPricePerPerson : 9800 
    minLos : 1 
    promotion : False 
    displayTotalPrice : 9800 
    locale : fr_FR 
    day : 2015-12-02 
    displayPricePerPerson : 9800 
    minLos : 1 
    promotion : False 
    displayTotalPrice : 9800 
    locale : fr_FR 
+0

是的,OP有什么是有效的ECMA脚本/ Javascript,而不是JSON。 – Dunes

+0

感谢您的答案,有没有办法将这个Javascriipt转换为JSON之前循环响应?我不确定我可以在机器人框架中使用stringify()吗? – Ziwdigforbugs

+0

@Ziwdigforbugs我已经将这些字典转换为json,只是将引号放在键的周围,并将false更改为False。所以它看起来应该把键放进引号中,而布尔值变成pythonic即大写。 – LetzerWille

1

假设实际的数据是有效的JSON(问题中的数据不是),一旦你调用To JSON,返回的数据就是一个可以迭代的字典列表。你应该能够在没有任何进一步转换的情况下循环播放它们。

例如:

${JSON}= To JSON ${resp.content} 
:FOR ${item} IN @{JSON} 
    \ log locale: ${item["locale"]} 

上面将记录在数据JSON每个字典一行。