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

2 投票
2 回答
2769 浏览
提问于 2025-04-17 18:56

我在Robot Framework中有一个变量,它的值是这样的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 个回答

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

在我的情况下,这个方法有效。

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

这就是你想要的东西吗?也许你需要多练习一下JSON了……

撰写回答