简体   繁体   English

如何在Java中创建二维json数组

[英]how to create two dimensional json array in java

Please help me. 请帮我。 I want to create like the following json array in java 我想在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"}
        ]
    };

Thanks for any help. 谢谢你的帮助。

Try this. 尝试这个。 First it deserialze your data and then it serialize your data. 首先,它反序列化您的数据,然后序列化您的数据。 I have used the famouse FasterXML Jackson library 我已经使用了著名的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 + '}';
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM