我有原始JSON字符串这样,其中我有键和值,如下所示 -如何使用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
}
现在我需要做的是,我要代表键值对为SH拥有以下。如果任何值是字符串数据类型(如键值u
),则用双引号表示它的值,否则不用双引号表示它的值。 country_id的含义值不会在String双引号中,因为它是一个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_id
和timestamp
这两个关键值是错误的。
所以现在我的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
}
所以纽约新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读取传入的数据它曾经双人间一切。要强制使用所需的类型,您需要修改'instanceof Map'情况的逻辑。基本上,提取键和值,然后在键值上有一个“开关”来为新类型分配数字。 – 2014-12-06 03:36:21
如果可能的话,重新设计JSON格式 - 这是没有意义的补充,如“串”冗余信息在那里 - 值已经是一个字符串,那么为什么添加无用的装饰? – StaxMan 2014-12-06 05:01:39
@StaxMan很抱歉,我现在无法更改JSON格式。我只需要坚持这种格式。任何建议我怎么能做到这一点? – john 2014-12-06 16:55:16