Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中创建二维json数组_Java_Arrays_Json - Fatal编程技术网

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

如何在java中创建二维json数组,java,arrays,json,Java,Arrays,Json,请帮帮我。我想用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: tr

请帮帮我。我想用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"}
        ]
    };

谢谢你的帮助。

试试这个。首先,它对数据进行反序列化,然后对数据进行序列化。我使用了著名的FasterXML Jackson库

import com.fasterxml.jackson.annotation.JsonInclude;
导入com.fasterxml.jackson.annotation.JsonInclude.Include;
导入java.io.IOException;
导入java.text.simpleDataFormat;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入org.codehaus.jackson.JsonParser;
导入org.codehaus.jackson.map.ObjectMapper;
公共类SimpleJson{
公共静态最终字符串JSON=“{\n”
+“数据:[\n”
+{id:1,文本:\“项目1\”,开始日期:\“31-03-2013\”,持续时间:3,进度:1,打开:true},\n
+{id:2,文本:\“任务1\”,开始日期:\“03-04-2013\”,持续时间:5,进度:1,打开:真,父项:1},\n
+{id:3,文本:\'Task#2\',开始日期:\'02-04-2013\',持续时间:7,进度:0.5,打开:true,父项:1},\n
+{id:4,文本:\'Task#2.1\',开始日期:\'03-04-2013\',持续时间:2,进度:1,打开:true,父级:3},\n
+{id:5,文本:\'Task#2.2\',开始日期:\'04-04-2013\',持续时间:3,进度:0.8,打开:true,父级:3},\n
+{id:6,文本:\“任务#2.3\”,开始日期:\“05-04-2013\”,持续时间:4,进度:0.2,打开:true,父级:3}\n
+“],\n”
+“链接:[\n”
+{id:1,源:1,目标:2,类型:\“1\”}\n
+{id:2,源:1,目标:3,类型:\“1\”}\n
+{id:3,源:3,目标:4,类型:\“1\”}\n
+{id:4,源:4,目标:5,类型:\“0\”}\n
+{id:5,源:5,目标:6,类型:\'0\'}\n
+“]\n”
+ "    }";
公共静态void main(字符串[]args)引发IOException{
ObjectMapper mapper=新的ObjectMapper();
configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,true);
setDateFormat(新的SimpleDateFormat(“dd-MM-yyyy”);
//从字符串读取反序列化数据
Task Task=mapper.readValue(JSON,Task.class);
System.out.println(“任务:”);
for(数据:task.getData()){
System.out.println(“数据:+数据);
}
for(链接:task.getLinks()){
System.out.println(“链接:+Link”);
}
//将数据序列化为字符串
字符串输出=mapper.writeValueAsString(任务);
System.out.println(“输出:”+输出);
}
公共静态类任务{
私有列表数据=新的ArrayList();
私有列表链接=新建ArrayList();
公共列表getData(){
返回数据;
}
公共无效设置数据(列表数据){
这个数据=数据;
}
公共列表getLinks(){
返回链接;
}
公共无效设置链接(列表链接){
this.links=链接;
}
}
@JsonInclude(Include.NON_NULL)
公共静态类数据{
私有int-id;
私有字符串文本;
私人日期开始日期;
私有整数持续时间;
私人双重进步;
私有布尔开放;
私有整数父;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getText(){
返回文本;
}
公共void setText(字符串文本){
this.text=文本;
}
公共日期getStart_日期(){
返回开始日期;
}
公共作废设置开始日期(日期开始日期){
this.start\u date=开始日期;
}
public int getDuration(){
返回时间;
}
公共void setDuration(int duration){
这个。持续时间=持续时间;
}
公共双getProgress(){
返回进度;
}
公共进度(双进度){
这个。进步=进步;
}
公共布尔等参元(){
返回打开;
}
公共void setOpen(布尔打开){
这个.打开=打开;
}
公共整数getParent(){
返回父母;
}
公共void setParent(整数父级){
this.parent=parent;
}
@凌驾
公共字符串toString(){
返回“数据{”+“id=“+id+”,text=“+text+”,start_date=“+start_date+”,duration=“+duration+”,progress=“+progress+”,open=“+open+”,parent=“+parent+}”;
}
}
公共静态类链接{
私有int-id;
私有int源;
私有int目标;
私有字符串类型;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
public int getSource(){
返回源;
}
公共void集合源(int源){
this.source=源;
}
公共int getTarget(){
回报目标;
}
公共无效集合目标(int目标){
this.target=目标;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
@凌驾
公共字符串toString(){
返回“Link{“+”id=“+id+”,source=“+source+”,target=“+target+”,type=“+type+'}”;
}
}
}

这不是有效的JSON。但这当然是可能的,这取决于您在Java中使用的JSON库。实际上,我发布的部分是javascript,我需要创建Json对象I
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 + '}';
        }
    }
}