2014-04-18 2 views
0

도와주세요. Java에서 다음 json 배열과 같이 만들고 싶습니다.자바에서 2 차원 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

을 사용했다. 하지만 물론 가능하며 자바에서 JSON 라이브러리를 사용하는 방식에 따라 다릅니다. – fge

+0

json-lib-2.4-jdk15.jar을 사용하고 있습니다. 사실 내가 게시 한 부분은 javascript에 있으며 java 액션에서 Json 객체를 생성해야하고 return 객체는 tasks 변수에 할당되어야합니다. – AK1

+0

유효한 json이지만 여기에 2 차원 배열이 없습니다. – Simulant

답변

0

시도해보십시오. 먼저 데이터를 비 직렬화 한 다음 데이터를 직렬화합니다. 나는이 유효 JSON하지 않은의 famouse FasterXML 잭슨 라이브러리

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 + '}'; 
     } 
    } 
} 
관련 문제