我检查了以下json,它是有效的,但是http://www.jsonschema2pojo.org/未将其转换为POJO对象,因此我可以从中获取值,在以下模型中需要“ nameValuePairs”对象。请帮忙。预先感谢
[
"order-chat-1",
{
"nameValuePairs":{
"chat":{
"nameValuePairs":{
"id":19,
"order_id":6,
"sender_id":10,
"receiver_id":3,
"message":"Hi",
"is_read":0,
"created_at":"2018-10-19 16:23:28",
"updated_at":"2018-10-19 16:23:28",
"is_sender":false
}
},
"message":"Hello from chef",
"message_type":"Message",
"is_sender":false
}
}
]
答案 0 :(得分:0)
从JavaScript的角度来看,它是有效的,但不适用于标准的Java库(例如GSON,Jackson或www.jsonschema2pojo.org
使用的任何库)。
有两件事:
顶部是一个数组
该数组String和一个对象有两种不同的类型
只能将其转换为Object[]
或Collection<Object>
(例如列表,设置等)。
但是要做到这一点,您必须有一个自定义的Serializer / Deserializer。
答案 1 :(得分:0)
这是要放在http://www.jsonschema2pojo.org/上的代码
{
"type":"object",
"properties":{
"chat":{
"type":"object",
"properties":{
"nameValuePairs":{
"type":"object",
"properties":{
"id": {"type": "integer"},
"order_id":{"type": "integer"},
"sender_id":{"type": "integer"},
"receiver_id":{"type": "integer"},
"message":{"type": "string"},
"is_read":{"type": "integer"},
"created_at":{"type": "string"},
"updated_at":{"type": "string"},
"is_sender":{"type": "boolean"}
}
}
}
},
"message":{"type": "string"},
"message_type":{"type": "string"},
"is_sender":{"type": "boolean"}
}
}
这将产生以下POJO,其中Example.class是您的根对象,请根据需要更改名称。
-----------------------------------com.example.Chat.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Chat {
@SerializedName("nameValuePairs")
@Expose
private NameValuePairs nameValuePairs;
public NameValuePairs getNameValuePairs() {
return nameValuePairs;
}
public void setNameValuePairs(NameValuePairs nameValuePairs) {
this.nameValuePairs = nameValuePairs;
}
public Chat withNameValuePairs(NameValuePairs nameValuePairs) {
this.nameValuePairs = nameValuePairs;
return this;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("chat")
@Expose
private Chat chat;
@SerializedName("message")
@Expose
private String message;
@SerializedName("message_type")
@Expose
private String messageType;
@SerializedName("is_sender")
@Expose
private boolean isSender;
public Chat getChat() {
return chat;
}
public void setChat(Chat chat) {
this.chat = chat;
}
public Example withChat(Chat chat) {
this.chat = chat;
return this;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Example withMessage(String message) {
this.message = message;
return this;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public Example withMessageType(String messageType) {
this.messageType = messageType;
return this;
}
public boolean isIsSender() {
return isSender;
}
public void setIsSender(boolean isSender) {
this.isSender = isSender;
}
public Example withIsSender(boolean isSender) {
this.isSender = isSender;
return this;
}
}
-----------------------------------com.example.NameValuePairs.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class NameValuePairs {
@SerializedName("id")
@Expose
private int id;
@SerializedName("order_id")
@Expose
private int orderId;
@SerializedName("sender_id")
@Expose
private int senderId;
@SerializedName("receiver_id")
@Expose
private int receiverId;
@SerializedName("message")
@Expose
private String message;
@SerializedName("is_read")
@Expose
private int isRead;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("is_sender")
@Expose
private boolean isSender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public NameValuePairs withId(int id) {
this.id = id;
return this;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public NameValuePairs withOrderId(int orderId) {
this.orderId = orderId;
return this;
}
public int getSenderId() {
return senderId;
}
public void setSenderId(int senderId) {
this.senderId = senderId;
}
public NameValuePairs withSenderId(int senderId) {
this.senderId = senderId;
return this;
}
public int getReceiverId() {
return receiverId;
}
public void setReceiverId(int receiverId) {
this.receiverId = receiverId;
}
public NameValuePairs withReceiverId(int receiverId) {
this.receiverId = receiverId;
return this;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public NameValuePairs withMessage(String message) {
this.message = message;
return this;
}
public int getIsRead() {
return isRead;
}
public void setIsRead(int isRead) {
this.isRead = isRead;
}
public NameValuePairs withIsRead(int isRead) {
this.isRead = isRead;
return this;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public NameValuePairs withCreatedAt(String createdAt) {
this.createdAt = createdAt;
return this;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public NameValuePairs withUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
return this;
}
public boolean isIsSender() {
return isSender;
}
public void setIsSender(boolean isSender) {
this.isSender = isSender;
}
public NameValuePairs withIsSender(boolean isSender) {
this.isSender = isSender;
return this;
}
}
答案 2 :(得分:0)
我使用另一种技术来解决此问题,我通过Object
将此String
转换为Gson()
,然后使用subString
方法删除了前16个字符,新创建的字符串的最后一个字符,然后将那个String
转换为POJO
对象。