使用Json和Java从数组获取对象

时间:2018-07-22 13:57:26

标签: activiti

我有一个json形式的表,其中包含对象此对象的变量之一是u对象我该如何获取它我将通过示例进行说明,我想获取变量使用Java我该怎么做

  {
 "data": [
  {
  "id": "40406",
  "processInstanceId": "40398",
   "processInstanceUrl": "http://10.6.99.57:8888/activiti- 
   rest/service/history/historic-process-instances/40398",
   "taskId": null,
   "variable": {
   "name": "nbj",
    "type": "string",
    "value": "3",
    "scope": null
     }
       },
    {
      "id": "40408",
     "processInstanceId": "40398",
        "processInstanceUrl": "http://10.6.99.57:8888/activiti- 
     rest/service/history/historic-process-instances/40398",
      "taskId": null,
      "variable": {
      "name": "nom",
      "type": "string",
      "value": "yasmine",
       "scope": null
        }
         },
         {
       "id": "40404",
        "processInstanceId": "40398",
         "processInstanceUrl": "http://10.6.99.57:8888/activiti- 
          rest/service/history/historic-process-instances/40398",
          "taskId": null,
           "variable": {
           "name": "reponse",
            "type": "string",
             "value": "1",
             "scope": null
               }
                }
                ], 

1 个答案:

答案 0 :(得分:0)

我猜您想执行rest调用并使用java读取rest响应。您可以使用Spring RestTemplate类和“ restTemplate.exchange”来执行调用。然后,您将需要使用jackson库读取响应,该库会将json转换为java对象-在这种情况下为java对象的集合。 Baeldung有restTemplatejackson的指南。您可以将Java类基于was originally used to generate the json的Java。您可能会发现设置@JsonIgnoreProperties(ignoreUnknown = true)很有用,这样您就不必在Java中为所有json建模。或者,您可以通过将响应读取为String开头,以便首先可以看到rest调用正在正确执行。