java 将 json 转换为对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39999420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert json to Object List
提问by Cristian
I have the following String:
我有以下字符串:
String json = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
And a SlaveEntity Entity that has:
还有一个 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;
}
I am writing a method that takes the json and returns a List of SlaveEntity:
我正在编写一个接受 json 并返回一个 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;
}
The problem is that the obj List results like this:
问题是 obj List 结果是这样的:
But what I need the obj List to be is like this:
但是我需要 obj List 是这样的:
So how can I get the needed list?
那么我怎样才能得到所需的清单呢?
回答by Mr. Polywhirl
You can convert the result to an object list, or you can pass in a type parameter rather than the List
class.
您可以将结果转换为对象列表,也可以传入类型参数而不是List
类。
String jsonString = "[{\"id\": \"0\", \"ip\": \"123\", \"mac\": \"456\"}, {\"id\": \"1\", \"ip\": \"111\", \"mac\": \"222\"}]";
With Object
和 Object
List<Object> items = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructParametricType(List.class, Object.class)
);
With SlaveEntity
和 SlaveEntity
List<SlaveEntity> items = objectMapper.readValue(
jsonString,
objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
);
Update
更新
This is what I have come up with, and it works.
这就是我想出的,并且有效。
EntityTest
实体测试
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.Hymanson.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>();
}
}
BaseEntity
基础实体
public class BaseEntity {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
SlaveEntity
从属实体
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import com.fasterxml.Hymanson.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);
}
}
PositionEntity
位置实体
public class PositionEntity {
// ?
}
Result
结果
SlaveEntity [id=0, ip=123, mac=456, status=null, positions=null]
SlaveEntity [id=1, ip=111, mac=222, status=null, positions=null]