如何从JSON字符串中获取所需内容

时间:2013-03-13 11:48:07

标签: python python-2.7 selenium-webdriver robotframework

我在Robot Frame Work中有一个变量,它具有以下格式的Json响应体

{
   "jsonrpc": "2.0",
   "id": "787878",
   "result":
   {
       "content":
       [
           {
               "id": 30,
               "userID": "user",
               "eventType":
               {
                   "name": "test"
               },
               "timestamp": "2013-03-13T11:00:28.537Z",
               "source": "Service",
               "reason": null,
               "comment": null,
               "dataCache":
               {
                   "lastLogin":
                   {
                       "id": 103,
                       "fieldName": "value1",
                       "newValue": "Wed Mar 13 07:00:28 EDT 2013",
                       "oldValue": null,
                       "type": "java.util.Date",
                       "auditEvent": null
                   }
               },
               "authority": "authenticate",
               "auditedObject": null
           }
       ],
       "pageNumber": 0,
       "pageSize": 99,
       "numberOfElements": 1,
       "totalElements": 1,
       "lastPage": true,
       "totalPages": 1
   }

}

从这里得到如何才能获得如下所示的datacache的内容

{
   "lastLogin":
   {
       "id": 103,
       "fieldName": "value1",
       "newValue": "Wed Mar 13 07:00:28 EDT 2013",
       "oldValue": null,
       "type": "java.util.Date",
       "auditEvent": null
   }
},

如果我使用像${variable['result']['content']}之类的变量,我会在内容中获得整个身体但我只想在“DataCache”中使用一个主体:

请帮我解决这个问题..

2 个答案:

答案 0 :(得分:5)

${variable['result']['content'][0]['dataCache']}

这是你要找的吗?也许你还需要一些JSON练习......

答案 1 :(得分:-1)

${variable['result']['content'][0]['dataCache']}

在我的案例中工作。