从JSON中提取元素的值

时间:2017-04-28 10:56:20

标签: java json getjson

我正在尝试进行测试,根据他们所属批次的ID获取一些文档。更具体地说,我想检查特定的batchPublicId是否在响应正文中。我正在使用okhttp进行测试。 这是json的缩短版本:

{
  "_embedded": {
    "invoices": [
      {
        "type": "INVOICE",
        "publicId": "27bc8426-17cf-4fe5-9278-64108ae05e4b",
        "deliveryStatus": null,
        "processingStatus": "INITIATED",
        "batchPublicId": "0000000000000000000000001"
      }
     ]
   }
}

我是json的新手,这就是我对这个问题的了解:

String invoicesJsonData = response.body().string();
JSONObject invoicesJsonObject = new JSONObject(invoicesJsonData);

Assert.assertTrue(invoicesJsonObject.getJSONObject("_embedded") !=null && invoicesJsonObject.getJSONObject("_embedded").has("invoices"));

我想验证batchPublicId是否具有json中提到的值。有没有办法做到这一点?谢谢。

6 个答案:

答案 0 :(得分:0)

String invoicesJsonData = response.body().string();
JSONObject invoicesJsonObject = new JSONObject(invoicesJsonData);
JSONObject invoicesJsonObject1 = invoicesJsonObject.getJSONObject("_embedded");
JSONArray f2=invoicesJsonObject1.getJSONArray("invoices");
 for(int i=0;i<f2.length();i++){

          JSONObject obj=f2.getJSONObject(i);
         if(obj.get("batchPublicId")!=null){
             System.out.println(obj.get("batchPublicId"));
         }

答案 1 :(得分:0)

你可以做这样的事情,有时候我会为我做好准备。

String invoicesJsonData = response.body().string();
JSONObject invoicesJsonObject = new JSONObject(invoicesJsonData);       

JSONObject invoicesJsonObject = json.getJSONObject("invoicesJsonObject");
String batchPublicId = invoicesJsonObject.getString("batchPublicId");
System.out.println( "batchPublicId: " + batchPublicId );
if(batchPublicId !=null){
// do something
}

不确定语法。给你一个提示。

答案 2 :(得分:0)

您可以检查json对象中是否有任何键,如下所示:

  if(jsonObject1.has("batchPublicId")){
  String batchPublicId = jsonObject1.optString("batchPublicId");
  Log.i(getClass().getSimpleName(), "batchPublicId=" + batchPublicId);}

has方法用于查找jsonobject中是否有任何关键字。

答案 3 :(得分:0)

在我看来,更好的方法是从这个JSON字符串创建一个POJO,并使用getters提取所需的信息

例如:

包装类:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "_embedded")
public class Embeded {

  @JsonProperty("invoices")
  private List<Invoice> invoices;

  public Embeded() {}

  public List<Invoice> getInvoices() {
    return invoices;
  }

  public void setInvoices(List<Invoice> invoices) {
    this.invoices = invoices;
  }
}

发票类:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice {

  @JsonProperty("type")
  private String type;
  @JsonProperty("publicId")
  private String publicId;
  @JsonProperty("deliveryStatus")
  private String deliveryStatus;
  @JsonProperty("processingStatus")
  private String processingStatus;
  @JsonProperty("batchPublicId")
  private String batchPublicId; 

  public Invoice() {}

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getPublicId() {
    return publicId;
  }

  public void setPublicId(String publicId) {
    this.publicId = publicId;
  }

  public String getDeliveryStatus() {
    return deliveryStatus;
  }

  public void setDeliveryStatus(String deliveryStatus) {
    this.deliveryStatus = deliveryStatus;
  }

  public String getProcessingStatus() {
    return processingStatus;
  }

  public void setProcessingStatus(String processingStatus) {
    this.processingStatus = processingStatus;
  }

  public String getBatchPublicId() {
    return batchPublicId;
  }

  public void setBatchPublicId(String batchPublicId) {
    this.batchPublicId = batchPublicId;
  }
}

测试:

  public void json_test() throws JsonParseException, JsonMappingException, IOException {
    String json = "{"
            +  "\"_embedded\": {"
            +    "\"invoices\": ["
            +    "{"
            +    "\"type\": \"INVOICE\","
            +   "\"publicId\": \"27bc8426-17cf-4fe5-9278-64108ae05e4b\","
            +  "\"deliveryStatus\": null,"
            +  "\"processingStatus\": \"INITIATED\","
            +  "\"batchPublicId\": \"0000000000000000000000001\""
            +   "}"
            +  "]"
            + "}"
            + "}";

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(Feature.UNWRAP_ROOT_VALUE, true);
    List<Invoice> invoices = mapper.readValue(json, Embeded.class).getInvoices();
    Assert.assertTrue(StringUtils.equals(invoices.get(0).getBatchPublicId(), "0000000000000000000000001"));
  }

答案 4 :(得分:-1)

如果我理解你的权利,你只需要打电话: Assert.assertTrue(invoicesJsonObject.getString("batchPublicId").equals("0000000000000000000000001"));"

答案 5 :(得分:-1)

如果要为JSON验证创建测试,可以使用JSONAssert。

JSONAsset给出方法assertEquals,它比较两个json结构,严格是identic还是not。

final String expected_result = YOUR_EXPECTED_RESULT;
JSONAssert.assertEquals(YOUR_EXPECTED_JSON_RESULT, RESULT_FROM_RESPONSE_BODY, false);

最后一个布尔参数定义您是否需要严格比较,或者只是比较您的预期结果是否来自响应。