将JSON字符串转换为Java对象
我有一个服务返回的字符串,格式如下:将JSON字符串转换为Java对象,java,json,gson,Java,Json,Gson,我有一个服务返回的字符串,格式如下: String message = { "Tickets": [{ "Type": "type1", "Author": "author1", "Rows": [ {
String message = {
"Tickets":
[{
"Type": "type1",
"Author": "author1",
"Rows":
[
{
"Price": "100.0",
"Date": "24/06/2016",
"Amount": "10"
},
{
"Type": "Comment",
"Value": "some comment goes here"
}
],
"ID": "165"
}],
"Desk": "desk1",
"User": "user1"
}
我需要解析它并将其转换为Java对象。
我尝试创建一个dom,如下所示:
public class TicketWrapper{
private Ticket ticket;
private String desk;
private String user;
}
public class Ticket {
private String type;
private String author;
private List<Row> rows;
private String id;
}
public class Row1{
private float price;
private Date date;
private int amount;
}
public class Row2{
private String type;
private float value;
}
但是如果我打印它System.out.println(gson.toJson(ticket))
,它会打印:
{“桌面”:0,“用户”:0}
我不知道如何将Json解析为Java对象,也不知道如何告诉他“Rows”中的行可以是Row1类型或Row2类型。正如其他人在评论中已经提到的,您需要确保映射直接反映文件名。它需要是“用户”和“桌面”,而不是“用户”和“桌面”。此外,您还有一个票证列表,它将映射到票证列表 正如其他人在评论中已经提到的,您需要确保映射直接反映文件名。它需要是“用户”和“桌面”,而不是“用户”和“桌面”。此外,您还有一个票证列表,它将映射到票证列表 我认为有一些问题,比如属性名称的小写和日期格式以及行的混合类型。我只是这样改变,为我工作:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class CheckTest {
@Test
public void thisTest() {
Gson gson = new GsonBuilder()
.setDateFormat("dd-MM-yyyy")
.setPrettyPrinting()
.create();
String message = "{\"Tickets\":" +
"[{\"Type\":\"type1\"," +
"\"Author\":\"author1\"," +
"\"Rows\":[{\"Price\":\"100.0\"," +
"\"Date\":\"24-06-2016\"," +
"\"Amount\":\"10\"}," +
"{\"Type\":\"Comment\"," +
"\"Value\":\"some comment goes here\"}]," +
"\"ID\":\"165\"}]," +
"\"Desk\":\"desk1\"," +
"\"User\":\"user1\"}";
TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class);
System.out.println(ticket.toString());
}
public class TicketWrapper {
@SerializedName("Tickets")
private List<Ticket> tickets;
@SerializedName("Desk")
private String desk;
@SerializedName("User")
private String user;
public TicketWrapper() {
}
}
public class Ticket {
@SerializedName("Type")
private String type;
@SerializedName("Author")
private String author;
@SerializedName("Rows")
private List<Row> rows;
@SerializedName("ID")
private String id;
public Ticket() {
}
}
public class Row {
@SerializedName("Type")
private String type;
@SerializedName("Value")
private String value;
@SerializedName("Price")
private float price;
@SerializedName("Date")
private Date date;
@SerializedName("Amount")
private int amount;
public Row() {
}
}
}
import com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.annotations.SerializedName;
导入org.junit.Test;
导入java.util.Date;
导入java.util.List;
公共类检查测试{
@试验
公开作废此测试(){
Gson Gson=new GsonBuilder()
.setDateFormat(“dd-MM-yyyy”)
.setPrettyPrinting()
.create();
字符串消息=“{\”票证\”:+
“[{\'Type\':\'type1\',”+
“\”作者\“:\”作者1\”+
“\'Rows\':[{\'Price\':\'100.0\',”+
““日期”:2016年6月24日”+
“\”金额\“:\”10\“}”+
“{\'类型\':\'注释\',”+
“\”值\“:\”此处有一些注释\“}],”+
“\“ID\:\“165\”}],”+
“\'Desk\':\'desk1\',”+
“\”用户\“:\”用户1\“}”;
TicketWrapper-ticket=gson.fromJson(消息,TicketWrapper.class);
System.out.println(ticket.toString());
}
公共类说唱歌手{
@序列化名称(“票证”)
私人名单门票;
@序列化名称(“桌面”)
私人弦乐台;
@SerializedName(“用户”)
私有字符串用户;
公共票务说唱人(){
}
}
公务舱票{
@序列化名称(“类型”)
私有字符串类型;
@序列化名称(“作者”)
私有字符串作者;
@SerializedName(“行”)
私有列表行;
@序列化名称(“ID”)
私有字符串id;
公众票{
}
}
公共类行{
@序列化名称(“类型”)
私有字符串类型;
@序列化名称(“值”)
私有字符串值;
@序列化名称(“价格”)
私人浮动价格;
@序列化名称(“日期”)
私人日期;
@序列化名称(“金额”)
私人整数金额;
公共行(){
}
}
}
我认为存在一些问题,例如属性名称的小写和日期格式以及行的混合类型。我只是这样改变,为我工作:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class CheckTest {
@Test
public void thisTest() {
Gson gson = new GsonBuilder()
.setDateFormat("dd-MM-yyyy")
.setPrettyPrinting()
.create();
String message = "{\"Tickets\":" +
"[{\"Type\":\"type1\"," +
"\"Author\":\"author1\"," +
"\"Rows\":[{\"Price\":\"100.0\"," +
"\"Date\":\"24-06-2016\"," +
"\"Amount\":\"10\"}," +
"{\"Type\":\"Comment\"," +
"\"Value\":\"some comment goes here\"}]," +
"\"ID\":\"165\"}]," +
"\"Desk\":\"desk1\"," +
"\"User\":\"user1\"}";
TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class);
System.out.println(ticket.toString());
}
public class TicketWrapper {
@SerializedName("Tickets")
private List<Ticket> tickets;
@SerializedName("Desk")
private String desk;
@SerializedName("User")
private String user;
public TicketWrapper() {
}
}
public class Ticket {
@SerializedName("Type")
private String type;
@SerializedName("Author")
private String author;
@SerializedName("Rows")
private List<Row> rows;
@SerializedName("ID")
private String id;
public Ticket() {
}
}
public class Row {
@SerializedName("Type")
private String type;
@SerializedName("Value")
private String value;
@SerializedName("Price")
private float price;
@SerializedName("Date")
private Date date;
@SerializedName("Amount")
private int amount;
public Row() {
}
}
}
import com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.google.gson.annotations.SerializedName;
导入org.junit.Test;
导入java.util.Date;
导入java.util.List;
公共类检查测试{
@试验
公开作废此测试(){
Gson Gson=new GsonBuilder()
.setDateFormat(“dd-MM-yyyy”)
.setPrettyPrinting()
.create();
字符串消息=“{\”票证\”:+
“[{\'Type\':\'type1\',”+
“\”作者\“:\”作者1\”+
“\'Rows\':[{\'Price\':\'100.0\',”+
““日期”:2016年6月24日”+
“\”金额\“:\”10\“}”+
“{\'类型\':\'注释\',”+
“\”值\“:\”此处有一些注释\“}],”+
“\“ID\:\“165\”}],”+
“\'Desk\':\'desk1\',”+
“\”用户\“:\”用户1\“}”;
TicketWrapper-ticket=gson.fromJson(消息,TicketWrapper.class);
System.out.println(ticket.toString());
}
公共类说唱歌手{
@序列化名称(“票证”)
私人名单门票;
@序列化名称(“桌面”)
私人弦乐台;
@SerializedName(“用户”)
私有字符串用户;
公共票务说唱人(){
}
}
公务舱票{
@序列化名称(“类型”)
私有字符串类型;
@序列化名称(“作者”)
私有字符串作者;
@SerializedName(“行”)
私有列表行;
@序列化名称(“ID”)
私有字符串id;
公众票{
}
}
公共类行{
@序列化名称(“类型”)
私有字符串类型;
@序列化名称(“值”)
私有字符串值;
@序列化名称(“价格”)
私人浮动价格;
@序列化名称(“日期”)
私人日期;
@序列化名称(“金额”)
私人整数金额;
公共行(){
}
}
}
大小写很重要。在JSON中,属性名为“Tickets”,带有一个s
,顾名思义,它是一个ticket数组,而不仅仅是一个ticket。还不清楚行是什么,以及行1和行2的相关性。我建议您阅读一些有关Gson用法的教程。FromJSONTANK you,我不知道大小写是否重要,因为按照惯例,变量以小写开头,但服务以大写形式返回变量:@SerializedName是解决方案!谢谢。大小写很重要。在JSON中,属性名为“Tickets”,带有一个s
,正如名称所示,它是一个Tickets数组,而不仅仅是一个ticket。还不清楚什么是行,以及第1行和第2行的相关性。我建议您阅读一些图图