java - 如何使用 GSON 保留 JSON 对象中特定键的数据类型?

标签 java json string gson

我有这样的原始 JSON 字符串,其中我有键和值,如下所示 -

{
  "u":{
     "string":"1235"
  },
  "p":"2047935",
  "client_id":{
     "string":"5"
  },
  "origin":null,
  "item_condition":null,
  "country_id":{
     "int":3
  },
  "timestamp":{
     "long":1417823759555
  },
  "impression_id":{
     "string":"2345HH*"
  },
  "is_consumerid":true,
  "is_pid":false
}

例如,一个键是“u”,它的值是-

{
    "string":"1235"
}

同样,另一个键是 "country_id",它的值是 -

{
    "int":3
}

现在我需要做的是,我需要如下所示表示键值对。如果任何值是字符串数据类型(如键 u 的值),则用双引号表示它的值,否则不要用双引号表示它的值。 country_id 的含义值不会在字符串双引号中,因为它是一个 int。

"u": "1235"
"p": "2047935"
"client_id": "5"
"origin":null
"item_condition":null
"country_id": 3 // I don't have double quotes here around 3 since country_id was int that's why
"timestamp": 1417823759555
"impression_id": "2345HH*"
"is_consumerid": true
"is_pid": false

然后我需要制作另一个 json 字符串,它应该如下所示 -

{
    "u": "1235",
    "p": "2047935",
    "client_id": "5",
    "origin":null,
    "item_condition":null,
    "country_id": 3,
    "timestamp": 1417823759555,
    "impression_id": "2345HH*",
    "is_consumerid": true,
    "is_pid": false
}

所以我开始了我的方法,并提供了以下代码 -

    String response = "original_json_string";
    Type type = new TypeToken<Map<String, Object>>() {}.getType();

    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();

    for (Map.Entry<String, JsonElement> object : jsonObject.entrySet()) {
        if (object.getValue() instanceof JsonObject) {
            String data = object.getValue().toString();
            Map<String, Object> jsonIn = gson.fromJson(data, type);
            Map<String, Object> jsonOut = new HashMap<String, Object>();

            Set<String> keys = jsonIn.keySet();
            for (String key : keys) {
                Object value = jsonIn.get(key);
                if (value instanceof Map) {
                    Map<?, ?> mapValue = (Map<?, ?>) value;
                    for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
                        jsonOut.put(key, entry.getValue());
                    }
                } else {
                    jsonOut.put(key, value);
                }
            }

            // System.out.println(jsonOut);

            String json = gson.toJson(jsonOut);
            System.out.println(json);

        }
    }

以上代码运行良好。唯一不起作用的是 - 当我尝试将 jsonOut 映射序列化为 JSON 时,某些键值未正确显示。比如country_idtimestamp这两个键值是错误的。

所以现在我的 json 是这样打印的 - 你可以看到,country_id 的值是 3.0 而不是它应该是 3。同样 timestamp 的值是 1.417823759555E12相反,它应该是 1417823759555

{
    "u": "1235",
    "p": "2047935",
    "client_id": "5",
    "origin":null,
    "item_condition":null,
    "country_id": 3.0,              // this is different value
    "timestamp": 1.417823759555E12, // and this is different value
    "impression_id": "2345HH*",
    "is_consumerid": true,
    "is_pid": false
}

所以 ny new json 应该像这样打印出来 -

{
    "u": "1235",
    "p": "2047935",
    "client_id": "5",
    "origin":null,
    "item_condition":null,
    "country_id": 3,
    "timestamp": 1417823759555,
    "impression_id": "2345HH*",
    "is_consumerid": true,
    "is_pid": false
}

我该怎么做,我做错了什么?

最佳答案

默认情况下,Gson 使用 Double 来映射任何 JSON 数字。由于您没有明确说明 Java 映射(即您使用了 Object)

Type type = new TypeToken<Map<String, Object>>() {}.getType();

Gson 将使用其默认值。

除非您指定确切的类型,这在您的情况下似乎很困难,因为您的 JSON 看起来是动态的,您需要自己进行转换,然后再将其添加到最终的序列化集合中。

关于java - 如何使用 GSON 保留 JSON 对象中特定键的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327838/

相关文章:

java - Hadoop:Eclipse 列表之外丢失数据

java - 如何将 EditText 字段中的文本添加到 android studio 中的不同 TextView

bash 中的字符串格式化

Java 套接字 : can you send from one thread and receive on another?

java - JDK > 8 中带有 OPTIONS header 的 Jersey JAX_RS 中存在错误?

mysql格式化mysql结果满足json handsontable

python - 将 json 对象数组转换为 tsv (python)

python - jsonschema 去掉未知字段

r - 计算数据串中的某些化学 react

c++ - 字符串格式异常 PRIu64