2014-04-28 2 views
2

JQuery FLOT 차트 플러그인에서 사용할 JSON 데이터 배열을 반환하는 Java REST 끝점을 만들려고합니다. 최소한Jackson2 배열을 만들 때 Java에서 Json 배열로 필드 이름을 무시합니다.

는 FLOT에 대한 JSON 데이터는 숫자의 배열이

[ [x1, y1], [x2, y2], ... ] 

을 즉 내가 자바 Point 객체의 목록을 감안할 때, 즉 포인트가

List<Point> data = new ArrayList<>(); 

정의된다 할 필요가 as

public class Point { 

    private final Integer x; 
    private final Integer y; 

    public Point(Integer x, Integer y) { 
     this.x = x; 
     this.y = y; 
    } 

    ... 
} 

JavaAS 객체를 삽입 할 필요가있는 메소드 또는 Jackson2 어노테이션 올바른 JSON 형식을 얻으십시오. 현재 내가이 형식으로 출력을 얻고있다 :

[{x:x1, y:y1}, {x:x2, y:y2} ...] 

을 나는이 형식이 필요한 경우 :

[[x1,y1], [x2,y2] ...] 
+0

당신이 필요로하는 것은 유효한 json이 아닙니다. – gipinani

+0

전체 구문보다는 형식 간의 차이점을 간단히 설명했습니다 : –

+0

@mserioli 최종 Flot 형식은 유효한 JSON ... 배열 배열 ... 트릭 Ayub는 x, y를 배열로 대 맵으로 반환하기 위해 사용자 지정 Jackson "Object Mapper/Resolver"가 필요합니다. (잭슨 용어가이 매퍼/리졸버에 대해 무엇인지 모르겠다.) – scunliffe

답변

1

import org.codehaus.jackson.map.annotate.JsonSerialize; 

@JsonSerialize(using = CustomPointSerializer.class) 
public class Point { 

    private Integer x; 
    private Integer y; 

    public Point(Integer x, Integer y) { 
     this.x = x; 
     this.y = y; 
    } 

    public Integer getX() { 
     return x; 
    } 

    public void setX(Integer x) { 
     this.x = x; 
    } 

    public Integer getY() { 
     return y; 
    } 

    public void setY(Integer y) { 
     this.y = y; 
    } 
} 

하고

ObjectMapper mapper = new ObjectMapper(); 
List<Point> points = new ArrayList<Point>(); 
points.add(new Point(1,2)); 
points.add(new Point(2,3)); 
System.out.println(mapper.writeValueAsString(points)); 

코드 시도는 다음과 같은 결과이 도움이

[[1,2],[2,3]] 

희망을 생산하고 있습니다.

+0

나는이 대답의 약간 수정 된 버전을 사용했으나 네 도움이된다. –

+0

나는 도움이 된다니 기쁩니다. – vzamanillo

1

당신은 intergers의 배열을 반환하는 특별한 getter 메소드에 @JsonView 주석을 사용할 수 있습니다. 다음은 예입니다

public class JacksonObjectAsArray { 
    static class Point { 

     private final Integer x; 
     private final Integer y; 

     public Point(Integer x, Integer y) { 
      this.x = x; 
      this.y = y; 
     } 

     @JsonValue 
     public int[] getXY() { 
      return new int[] {x, y}; 
     } 
    } 

    public static void main(String[] args) throws JsonProcessingException { 
     ObjectMapper mapper = new ObjectMapper(); 
     System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Point(12, 45))); 
    } 

} 

출력 : 당신은

import java.io.IOException; 

import org.codehaus.jackson.JsonGenerator; 
import org.codehaus.jackson.JsonProcessingException; 
import org.codehaus.jackson.map.JsonSerializer; 
import org.codehaus.jackson.map.SerializerProvider; 

public class CustomPointSerializer extends JsonSerializer<Point> { 

    @Override 
    public void serialize(Point point, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException { 
     gen.writeStartArray(); 
     gen.writeNumber(point.getX()); 
     gen.writeNumber(point.getY()); 
     gen.writeEndArray(); 
    } 
} 

은 당신이 당신의 Point 클래스에 사용자 정의 시리얼 클래스를 설정할 수있는 사용자 정의 Point 시리얼 라이저를 작성할 수

[ 12, 45 ] 
관련 문제