将JSON字符串转换为Java对象

时间:2016-04-06 20:45:52

标签: java json gson

我有一个服务返回的字符串,采用以下JSON格式:

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;
}

我需要解析它并转换为Java对象。 我试图创建一个这样的dom:

TicketWrapper ticket = gson.fromJson(message, TicketWrapper.class)

然后我尝试用Google Gson解析它,这样:

System.out.println(gson.toJson(ticket))

但是如果我打印它 NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]]; NSString *urlString = [pictureURL absoluteString]; FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]; [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if (!error) { // result is a dictionary with the user's Facebook data NSDictionary *userData = (NSDictionary *)result; NSString *facebookID = userData[@"id"]; NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]]; NSString *urlString = [pictureURL absoluteString]; NSLog(@"sting=%@",urlString); PFUser *me = [PFUser currentUser]; me[@"facebookId"] = userData[@"id"]; me["pictureURL"] = userData[urlString]; me[@"username"] = userData[@"name"]; [me saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) { if (error) { NSLog(@"Error to store=%@",error.localizedDescription); } }]; [self presentViewController:push animated:YES completion:nil]; } else { [self presentViewController:push animated:YES completion:nil]; } }]; ,它会打印: {&#34;台&#34; :0,&#34;用户&#34; :0}

我不知道如何将Json解析为Java对象,以及如何告诉他一行进入&#34; Rows&#34;可以是Row1类型或Row2类型。

2 个答案:

答案 0 :(得分:1)

正如其他人在评论中已经提到的那样,您需要确保映射直接反映文件名。它需要是&#39; User&#39;和&#39; Desk&#39;而不是&#39;用户&#39;和&#39; desk&#39;。此外,您还有一个故障单列表,它将映射到列表故障单。

答案 1 :(得分:1)

我认为存在一些问题,例如小写和dateformat属性的名称以及行的混合类型。我刚刚改变了这个并为我工作:

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() {
        }
    }
}