从REST解析JSON对象和数组

时间:2015-04-22 23:02:10

标签: java json rest jax-rs

我有以下JSON,我想从REST解析为JAVA。我有一个AR对象和一个方法,并以葡萄酒为参数。我该怎么做?

 {
    "type": "AR",
    "selection_month": "Feb/2015",
    "wines": [
      {
          "variety": "RED",
          "wine_type": "TABLE",
          "label_name": "The Mission",
          "grape": "Merlot",
          "region": "Napa",
          "country": "USA",
          "maker": "Sterling",
          "year": "2014"
      },
      {
          "variety": "RED",
          "wine_type": "TABLE",
          "label_name": "Joseph Phelps Cabernet Sauvignon 2012",
          "grape": "Cabernet Sauvignon",
          "region": "Napa",
          "country": "USA",
          "maker": "Joseph Phelps",
          "year": "2012"
      },
      {
          "variety": "RED",
          "wine_type": "TABLE",
          "label_name": "Round Pond Estate Rutherford",
          "grape": "Cabernet Sauvignon",
          "region": "Napa",
          "country": "USA",
          "maker": "Rutherford",
          "year": "2014"
      },
      {
          "variety": "RED",
          "wine_type": "TABLE",
          "label_name": "Dona Paula Black Label",
          "grape": "Bordeaux",
          "region": "",
          "country": "Argentina",
          "maker": "Dona Paula",
          "year": "2013"
      },
      {
          "variety": "RED",
          "wine_type": "TABLE",
          "label_name": "Schug Sonoma Coast Pinot Noir",
          "grape": "Pinot Noir",
          "region": "Sonoma Valley",
          "country": "USA",
          "maker": "Walter Schug",
          "year": "2013"
      },
      {
          "variety": "RED",
          "wine_type": "TABLE",
          "label_name": "Caymus Special Selection Cabernet Sauvignon",
          "grape": "Cabernet Sauvignon",
          "region": "Napa Valley",
          "country": "USA",
          "maker": "Charles F. Wagner",
          "year": "2014"
      }
    ]
  }

我有以下方法

@POST
public Response addAR(InputStream incomingData){
    String parsedJson = jsonStreamToString(incomingData);
    try {
        JSONObject json = new JSONObject(parsedJson);

        String name = json.getString("name");


        int uid = useCase.createAR(name);

        JSONObject jsonResponse = new JSONObject(new JSONStringer().object()
                .key("id").value(uid).endObject().toString());

        return Response.status(201).entity(jsonResponse.toString()).build();
    } catch (JSONException e) {

        e.printStackTrace();
        return Response.status(404).entity(e.toString()).build();
    }
}

1 个答案:

答案 0 :(得分:0)

我建议不要自己解析所有内容,并使用POJO。让Jackson将JSON解析为您的POJO。 Wine可以是一个类,AR可以是具有List<Wine>

的类
  

&#34;我正在使用Jersey JAX_RS 2.0&#34;

要获得Jackson支持,如果您使用的是Maven,只需添加此依赖

即可
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

如果您不使用Maven,请下载这些jar

  • jersey-media-json-jackson - 选择版本(与Jersey 2.x版本匹配,然后下载)

  • 从下图中获取所有内容

    enter image description here

    我在v2.2.3的另一篇文章中有一张图片 - 忽略了版本。你想要获得所有版本的2.3.2版本。对于第一个依赖项,它们都可以在上面的链接中找到。只需在搜索栏中搜索它们即可。找到后,选择版本并下载。

拥有所有jar /依赖项后,您只需使用从JSON转换的POJO即可。例如,您可以拥有Wine

public class Wine {
    private String variety;
    @JsonProperty("wine_type")
    private String wineType;
    @JsonProperty("label_name")
    private String labelName;
    private String grape;
    private String country;
    private String maker;
    private String year;

    // proper Getters and Setters mathing for form
    // public String getVariety()
    // public void setVariety(String variety)
    // basically following JavaBean naming convention
}

您将需要适当的JavaBean样式getter和setter。您会注意到有几个字段有@JsonProperty注释。这是因为_(下划线)的JSON属性不能很好地映射到方法,所以我们需要命名属性。

然后,您可以拥有另一个具有List<Wine>的类,以及顶级JSON对象的其他属性。例如

public class AR {
    private String type;
    @JsonProperty("selection_month")
    private String selectionMonth;
    private List<Wine> wines;

    // Getters and Setters
}

现在您可以只使用AR类型作为方法参数。您可以直接使用AR对象

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addAR(AR ar){