我是新手,我用简单的json实现了保存数据,但现在我有一个带有多个对象和数组的json,我花了很多时间尝试实现这一点。任何帮助将不胜感激。
示例json如下:
{
"status": 200,
"message": "Success",
"data": [
{
"containerStatus2": "Loaded",
"stops": [
{
"city": "ADDSTOPCITY",
"id": "70"
}
],
"containerStatus": "Loaded",
"instructions": "testing load",
"dispatcher": {
"status": 0,
"image_url": "https://lorempixel.com/200/200/technics/6",
"os": ""
},
"docs": [
{
"link": "https://alfred.urbantrucking.com/static/uploads/hhzgkplbqdec.jpg",
"type": "POD",
"name": "marijuana-map-large.jpg"
},
{
"type": "BOL",
"link": "https://alfred.urbantrucking.com/static/uploads/jttojntemolt.jpg",
"name": "lab-test.jpg"
}
],
"created": "Jan-01-2018, 09:36 PM",
"pods": [],
"container2": ""
}],
"success": 1
}
答案 0 :(得分:1)
http://www.jsonschema2pojo.org/
-----------------------------------com.example.Datum.java-----------------------------------
public class Datum extends RealmObject{
private String containerStatus2;
private List<Stop> stops = null;
private String containerStatus;
private String instructions;
private Dispatcher dispatcher;
private List<Doc> docs = null;
private String created;
private List<Object> pods = null;
private String container2;
public String getContainerStatus2() {
return containerStatus2;
}
public void setContainerStatus2(String containerStatus2) {
this.containerStatus2 = containerStatus2;
}
public List<Stop> getStops() {
return stops;
}
public void setStops(List<Stop> stops) {
this.stops = stops;
}
public String getContainerStatus() {
return containerStatus;
}
public void setContainerStatus(String containerStatus) {
this.containerStatus = containerStatus;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public Dispatcher getDispatcher() {
return dispatcher;
}
public void setDispatcher(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
}
public List<Doc> getDocs() {
return docs;
}
public void setDocs(List<Doc> docs) {
this.docs = docs;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public List<Object> getPods() {
return pods;
}
public void setPods(List<Object> pods) {
this.pods = pods;
}
public String getContainer2() {
return container2;
}
public void setContainer2(String container2) {
this.container2 = container2;
}
}
----------------------------------- com.example.Dispatcher.java ----- ------------------------------
public class Dispatcher extends RealmObject{
private int status;
private String imageUrl;
private String os;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
----------------------------------- com.example.Doc.java ----- ------------------------------
public class Doc extends RealmObject{
private String link;
private String type;
private String name;
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------------------------------- com.example.Response.java ----- ------------------------------
public class Response extends RealmObject {
private int status;
private String message;
private List<Datum> data = null;
private int success;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
}
----------------------------------- com.example.Stop.java ----- ------------------------------
public class Stop extends RealmObject{
private String city;
private String id;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
https://realm.io/docs/java/latest/
并保存:
Realm realm = Realm.getInstance(this);
realm.beginTransaction();
Response u = realm.createObject(Response.class);
u.setMessage("Hello response");
realm.commitTransaction();