请帮帮我。我想在java
中创建类似下面的json数组 var tasks = {
data:[
{id:1, text:"Project #1",start_date:"31-03-2013", duration:3, progress: 1, open: true},
{id:2, text:"Task #1", start_date:"03-04-2013", duration:5, progress: 1, open: true, parent:1},
{id:3, text:"Task #2", start_date:"02-04-2013", duration:7, progress: 0.5, open: true, parent:1},
{id:4, text:"Task #2.1", start_date:"03-04-2013", duration:2, progress: 1, open: true, parent:3},
{id:5, text:"Task #2.2", start_date:"04-04-2013", duration:3, progress: 0.8, open: true, parent:3},
{id:6, text:"Task #2.3", start_date:"05-04-2013", duration:4, progress: 0.2, open: true, parent:3}
],
links:[
{id:1, source:1, target:2, type:"1"},
{id:2, source:1, target:3, type:"1"},
{id:3, source:3, target:4, type:"1"},
{id:4, source:4, target:5, type:"0"},
{id:5, source:5, target:6, type:"0"}
]
};
感谢您的帮助。
答案 0 :(得分:0)
试试这个。首先,它反序列化您的数据,然后序列化您的数据。我使用了着名的 FasterXML Jackson 库
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
public class SimpleJson {
public static final String JSON = "{\n"
+ " data:[\n"
+ " {id:1, text:\"Project #1\",start_date:\"31-03-2013\", duration:3, progress: 1, open: true},\n"
+ " {id:2, text:\"Task #1\", start_date:\"03-04-2013\", duration:5, progress: 1, open: true, parent:1},\n"
+ " {id:3, text:\"Task #2\", start_date:\"02-04-2013\", duration:7, progress: 0.5, open: true, parent:1},\n"
+ " {id:4, text:\"Task #2.1\", start_date:\"03-04-2013\", duration:2, progress: 1, open: true, parent:3},\n"
+ " {id:5, text:\"Task #2.2\", start_date:\"04-04-2013\", duration:3, progress: 0.8, open: true, parent:3},\n"
+ " {id:6, text:\"Task #2.3\", start_date:\"05-04-2013\", duration:4, progress: 0.2, open: true, parent:3}\n"
+ " ],\n"
+ " links:[\n"
+ " {id:1, source:1, target:2, type:\"1\"},\n"
+ " {id:2, source:1, target:3, type:\"1\"},\n"
+ " {id:3, source:3, target:4, type:\"1\"},\n"
+ " {id:4, source:4, target:5, type:\"0\"},\n"
+ " {id:5, source:5, target:6, type:\"0\"}\n"
+ " ]\n"
+ " }";
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
// Read deserialize data from String
Task task = mapper.readValue(JSON, Task.class);
System.out.println("Task: ");
for (Data data : task.getData()) {
System.out.println("Data: " + data);
}
for (Link link : task.getLinks()) {
System.out.println("Link: " + link);
}
// Serialize data to String
String output = mapper.writeValueAsString(task);
System.out.println("OUTPUT: " + output);
}
public static class Task {
private List<Data> data = new ArrayList<>();
private List<Link> links = new ArrayList<>();
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public List<Link> getLinks() {
return links;
}
public void setLinks(List<Link> links) {
this.links = links;
}
}
@JsonInclude(Include.NON_NULL)
public static class Data {
private int id;
private String text;
private Date start_date;
private int duration;
private double progress;
private boolean open;
private Integer parent;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getStart_date() {
return start_date;
}
public void setStart_date(Date start_date) {
this.start_date = start_date;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public double getProgress() {
return progress;
}
public void setProgress(double progress) {
this.progress = progress;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public Integer getParent() {
return parent;
}
public void setParent(Integer parent) {
this.parent = parent;
}
@Override
public String toString() {
return "Data{" + "id=" + id + ", text=" + text + ", start_date=" + start_date + ", duration=" + duration + ", progress=" + progress + ", open=" + open + ", parent=" + parent + '}';
}
}
public static class Link {
private int id;
private int source;
private int target;
private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
public int getTarget() {
return target;
}
public void setTarget(int target) {
this.target = target;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Link{" + "id=" + id + ", source=" + source + ", target=" + target + ", type=" + type + '}';
}
}
}