简体   繁体   English

从REST解析JSON对象和数组

[英]Parsing an JSON Object and Array from REST

I have the following JSON that I would like to parse into JAVA from REST. 我想将以下JSON从REST解析为JAVA。 I have an AR object and a method and takes wine an argument. 我有一个AR对象和一个方法,并以wine为参数。 How would I do it? 我该怎么办?

 {
    "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"
      }
    ]
  }

I have the following method going 我有以下方法

@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();
    }
}

I'd suggest not trying to parse everything yourself, and use POJOs. 我建议不要尝试自己解析所有内容,并使用POJO。 Let Jackson parse the JSON into your POJO. 让Jackson将JSON解析到您的POJO中。 Wine could be a class and AR whatever that is can be a class with a List<Wine> Wine可以是一个类,而AR可以是具有List<Wine>

"I am using Jersey JAX_RS 2.0" “我正在使用Jersey JAX_RS 2.0”

To get Jackson support, if you are using Maven, simply add this dependency 要获得Jackson支持,如果您使用的是Maven,只需添加此依赖项

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

If you're not using Maven, then download these jars 如果您不使用Maven,请下载这些jar

  • jersey-media-json-jackson - Select the version (matching your Jersey 2.x version, then download) jersey-media-json-jackson-选择版本(与您的Jersey 2.x版本匹配,然后下载)

  • Get all the ones from the below image 从下图获取所有内容

    在此处输入图片说明

    I had an image from another post with v2.2.3- disregard the version. 我从v2.2.3-的另一个帖子中获得了一个图像,而忽略了该版本。 The version you want to get 2.3.2 for all of them. 您希望所有版本都获得2.3.2的版本。 They can all be found at the above link, for the first dependency. 对于第一个依赖项,都可以在上面的链接中找到它们。 Just search for them in the search bar. 只需在搜索栏中搜索它们即可。 When you find it, select the version and download it. 找到它后,选择版本并下载。

Once you have all the jars/dependency you can simply use POJOs converted from your JSON. 一旦有了所有的jar /依赖关系,您就可以简单地使用从JSON转换的POJO。 For example you can have a Wine class 例如,您可以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
}

You will need proper JavaBean style getters and setters. 您将需要适当的JavaBean风格的getter和setter。 You'll notice a couple of the fields have @JsonProperty annotation. 您会注意到其中两个字段具有@JsonProperty批注。 That is because JSON properties with _ (underscore) don't map well to the methods, so we need to name the property. 这是因为带有_ (下划线)的JSON属性无法很好地映射到方法,因此我们需要命名该属性。

Then you can have another class that has a List<Wine> , along with the other properties of the top level JSON object. 然后,您可以拥有另一个具有List<Wine> ,以及顶级JSON对象的其他属性。 for example 例如

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

    // Getters and Setters
}

Now you can simply have an AR type as the method parameter. 现在,您可以简单地将AR类型用作方法参数。 An you can work with the AR object directly 您可以直接使用AR对象

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

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

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