将 json 转换为对象列表
Convert json to Object List
我有以下字符串:
String json = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
一个 SlaveEntity 实体具有:
public class SlaveEntity extends BaseEntity {
private String ip;
private String macAddress;
private String status;
@OneToMany(mappedBy="slave", targetEntity = PositionEntity.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<PositionEntity> positions;
}
我正在编写一个采用 json 和 returns SlaveEntity 列表的方法:
public static List<SlaveEntity> JsonToSlaveEntity(String json) {
ObjectMapper objectMapper = new ObjectMapper();
List<SlaveEntity> obj = new ArrayList<SlaveEntity>();
try {
obj = objectMapper.readValue(json, List.class);
} catch (IOException e) {
e.printStackTrace();
}
return obj;
}
问题是 obj 列表的结果是这样的:
但我需要的对象列表是这样的:
那么我怎样才能得到需要的列表呢?
您可以将结果转换为对象列表,或者您可以传入类型参数而不是 List
class。
String jsonString = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
和Object
List<Object> items = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructParametricType(List.class, Object.class)
);
与SlaveEntity
List<SlaveEntity> items = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
);
更新
这是我想出来的,而且有效。
实体测试
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EntityTest {
public static void main(String[] args) {
String json = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
for (SlaveEntity entity : jsonToSlaveEntity(json)) {
System.out.println(entity);
}
}
public static List<SlaveEntity> jsonToSlaveEntity(String json) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(
json,
objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
);
} catch (IOException e) {
e.printStackTrace();
}
return new ArrayList<SlaveEntity>();
}
}
基础实体
public class BaseEntity {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
奴隶实体
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonProperty;
public class SlaveEntity extends BaseEntity {
private String ip;
@JsonProperty("mac")
private String macAddress;
private String status;
@OneToMany(mappedBy = "slave", targetEntity = PositionEntity.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<PositionEntity> positions;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<PositionEntity> getPositions() {
return positions;
}
public void setPositions(List<PositionEntity> positions) {
this.positions = positions;
}
@Override
public String toString() {
return String.format(
"SlaveEntity [id=%d, ip=%s, mac=%s, status=%s, positions=%s]",
getId(), ip, macAddress, status, positions);
}
}
位置实体
public class PositionEntity {
// ?
}
结果
SlaveEntity [id=0, ip=123, mac=456, status=null, positions=null]
SlaveEntity [id=1, ip=111, mac=222, status=null, positions=null]
我有以下字符串:
String json = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
一个 SlaveEntity 实体具有:
public class SlaveEntity extends BaseEntity {
private String ip;
private String macAddress;
private String status;
@OneToMany(mappedBy="slave", targetEntity = PositionEntity.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<PositionEntity> positions;
}
我正在编写一个采用 json 和 returns SlaveEntity 列表的方法:
public static List<SlaveEntity> JsonToSlaveEntity(String json) {
ObjectMapper objectMapper = new ObjectMapper();
List<SlaveEntity> obj = new ArrayList<SlaveEntity>();
try {
obj = objectMapper.readValue(json, List.class);
} catch (IOException e) {
e.printStackTrace();
}
return obj;
}
问题是 obj 列表的结果是这样的:
但我需要的对象列表是这样的:
那么我怎样才能得到需要的列表呢?
您可以将结果转换为对象列表,或者您可以传入类型参数而不是 List
class。
String jsonString = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
和Object
List<Object> items = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructParametricType(List.class, Object.class)
);
与SlaveEntity
List<SlaveEntity> items = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
);
更新
这是我想出来的,而且有效。
实体测试
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EntityTest {
public static void main(String[] args) {
String json = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
for (SlaveEntity entity : jsonToSlaveEntity(json)) {
System.out.println(entity);
}
}
public static List<SlaveEntity> jsonToSlaveEntity(String json) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(
json,
objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
);
} catch (IOException e) {
e.printStackTrace();
}
return new ArrayList<SlaveEntity>();
}
}
基础实体
public class BaseEntity {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
奴隶实体
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonProperty;
public class SlaveEntity extends BaseEntity {
private String ip;
@JsonProperty("mac")
private String macAddress;
private String status;
@OneToMany(mappedBy = "slave", targetEntity = PositionEntity.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<PositionEntity> positions;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<PositionEntity> getPositions() {
return positions;
}
public void setPositions(List<PositionEntity> positions) {
this.positions = positions;
}
@Override
public String toString() {
return String.format(
"SlaveEntity [id=%d, ip=%s, mac=%s, status=%s, positions=%s]",
getId(), ip, macAddress, status, positions);
}
}
位置实体
public class PositionEntity {
// ?
}
结果
SlaveEntity [id=0, ip=123, mac=456, status=null, positions=null]
SlaveEntity [id=1, ip=111, mac=222, status=null, positions=null]