简体   繁体   English

java jsonobject“value”和“num”

[英]java jsonobject "value" and "num"

when using the javax json ObjectBuilder to build json, the output json string contains "value" and "num" in the json string.使用javax json ObjectBuilder构建json时,输出的json字符串在json字符串中包含“value”和“num”。 my web service is throw serialization error when the json contains "value" and "num".当 json 包含“value”和“num”时,我的 Web 服务抛出序列化错误。 any one know why the output have "value" and "num"?有人知道为什么输出有“value”和“num”吗?

example:例子:

JsonObject addProductRequest = Json.createObjectBuilder()
            .add("OrderID", orderId)
            .add("Product", productId)
            .add("Quantity", qty)
            .add("access_token", access_token)
            .build();

output:输出:

{
  "OrderID": {
    "num": 15498
  },
  "ProductID": {
    "num": 20
  },
  "Quantity": {
    "num": 1
  },
  "access_token": {
    "value": "1f6c689d-6956-4f8e-b259-9030d57bec4b"
  }
}

when I switch to using google.gson.JsonObject, the output dont have the "value" and "num" string, which my web service accepts and every thing seems to work fine.当我切换到使用 google.gson.JsonObject 时,输出没有“value”和“num”字符串,我的网络服务接受这些字符串,并且一切似乎都正常。 example例子

 com.google.gson.JsonObject addProductRequest = new com.google.gson.JsonObject();
    addProductRequest.addProperty("OrderID", orderId);
    addProductRequest.addProperty("Product", productId);
    addProductRequest.addProperty("Quantity", qty);
    addProductRequest.addProperty("access_token", access_token);

output:输出:

{  "OrderID": 15499,  "Product": 20,  "Quantity": 1,  "access_token": "3241cfd4-7b6c-4eac-b2bb-9b2b0c780831"}

Rest Assured seems to use Gson to serialize POJOs (which is what you should be using as response entities) to the response body. Rest Assured 似乎使用 Gson 将 POJO(这是您应该用作响应实体的)序列化到响应正文。

Gson doesn't know anything about javax.json . Gson 对javax.json The implementation of javax.json that you are using basically has this format for its JsonObject :您正在使用的javax.json的实现对于其JsonObject基本上具有以下格式:

private static final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject {
    private final Map<String, JsonValue> valueMap;      // unmodifiable

Since this is a Map , Gson uses special serializing, iterating through its entrySet and using each Entry as a JSON key-value pair (within a JSON object).由于这是一个Map ,Gson 使用特殊的序列化,遍历其entrySet并将每个Entry用作 JSON 键值对(在 JSON 对象中)。

In this case, the entrySet returns在这种情况下, entrySet返回

@Override
public Set<Entry<String, JsonValue>> entrySet() {
    return valueMap.entrySet();
} 

where the valueMap contains all the values you added with add in your builder.其中valueMap包含您在构建器中add所有值。 For example, for例如,对于

.add("OrderID", 1)

it will have added an entry with the String key OrderID and the JsonValue value of它将添加一个带有StringOrderIDJsonValue值的条目

// Optimized JsonNumber impl for int numbers.
private static final class JsonIntNumber extends JsonNumberImpl {
    private final int num;

Notice the num field.注意num字段。

So Gson will now see a value of this JsonIntNumber type.所以 Gson 现在将看到这个JsonIntNumber类型的值。 It considers it a POJO and so serializes it as a JSON object with its fields as key-value pairs.它认为它是一个 POJO,因此将它序列化为一个 JSON 对象,其字段作为键值对。 It therefore produces因此它产生

{
    "num": 15498
}

com.google.gson.JsonObject is a known type to Gson . com.google.gson.JsonObjectGson的已知类型。 It knows that it is special and not a custom POJO type.它知道它是特殊的,而不是自定义的 POJO 类型。 It can therefore write it directly, instead of having to serialize it further.因此,它可以直接编写它,而不必进一步序列化它。


This is similar to a question which I answered here:这类似于我在这里回答的一个问题:

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

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