使用java消耗json模型

时间:2018-04-15 14:47:54

标签: java json spring rest jax-rs

人们,我需要帮助,如果有人能帮助我,我感谢你!

我正在获取JSON并想知道我是否可以将信息提供给我的模型列表。

是发送1条短信的电话

这是我第一次尝试使用JSON

我的模特拥有所有吸气剂和制定者。

模型

public class Zenvia {

@JsonProperty("sendSmsResponse")
private String sendSmsResponse;

//Request
@JsonProperty("id")
private String id;
@JsonProperty("from")
private String from;
@JsonProperty("to")
private String to;
@JsonProperty("msg")
private String msg;
@JsonProperty("schedule")
private String schedule;
@JsonProperty("callbackOption")
private String callbackOption;
@JsonProperty("aggregateId")
private String aggregateId;
@JsonProperty("flashSms")
private boolean flashSms;

//Response
private long stats;
@JsonProperty("statusCode")
private String statusCode;
@JsonProperty("statusDescription")
private String statusDescription;
@JsonProperty("detailCode")
private String detailCode;
@JsonProperty("detailDescription")
private String detailDescription;
@JsonProperty("mobileOperatorName")
private String mobileOperatorName;
@JsonProperty("received")
private String received;

//getters and setters
}

我的电话

public Zenvia senderUnique(Zenvia zenvia) {

        Response response = null;
        try {

            Client client = ClientBuilder.newClient();
            Entity payload = Entity.json("    {\n"
                    + "        \"sendSmsRequest\": {\n"
                    + "            \"from\": \"" + zenvia.getFrom() + "\",\n"
                    + "            \"to\": \"" + zenvia.getTo() + "\",\n"
                    + "            \"schedule\": \"" + zenvia.getSchedule() + "\",\n"
                    + "            \"msg\": \"" + zenvia.getMsg() + "\",\n"
                    + "            \"callbackOption\": \"" + zenvia.getCallbackOption() + "\",\n"
                    + "            \"id\": \"" + zenvia.getId() + "\",\n"
                    + "            \"aggregateId\": \"" + zenvia.getAggregateId() + "\",\n"
                    + "            \"flashSms\": " + zenvia.isFlashSms() + "\n"
                    + "        }\n"
                    + "    }");

            response = client.target("https://api-rest.zenvia360.com.br/services/send-sms")
                    .request(MediaType.APPLICATION_JSON_TYPE)
                    .header("Authorization", "Basic ***********")
                    .header("Accept", "application/json")
                    .post(payload);

            System.out.println("status: " + response.getStatus());
            System.out.println("headers: " + response.getHeaders());
            System.out.println("body:" + response.readEntity(String.class));

            response.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zenvia;
    }

此时我有这个回报,这是消费这些数据的最佳做法,我有一个模型准备接收这些信息。

转换为LIST? 为α

string JSON response that im getting

        body:{
  "sendSmsMultiResponse" : {
    "sendSmsResponseList" : [ {
      "statusCode" : "10",
      "statusDescription" : "Error",
      "detailCode" : "080",
      "detailDescription" : "Message with same ID already sent"
    }, {
      "statusCode" : "10",
      "statusDescription" : "Error",
      "detailCode" : "080",
      "detailDescription" : "Message with same ID already sent"
    }, {
      "statusCode" : "10",
      "statusDescription" : "Error",
      "detailCode" : "080",
      "detailDescription" : "Message with same ID already sent"
    } ]
  }
}

可以是一个或多个结果,列表也是如此。

1 个答案:

答案 0 :(得分:0)

您可以使用String

阅读对Map的回复,然后Entityjackson班级的回复
ObjectMapper mapper = new ObjectMapper();
String jsonInString = response.readEntity(String.class);

//Convert JSON from String to Object
List<YourEntityModelObject> objList = mapper.readValue(jsonInString, new TypeReference<List<YourEntityModelObject>>(){});