Java JSON映射<;字符串>;每个基本数据类型的转换
此代码的目的是将未知数量的参数和未知数量的值转换为Java JSON映射<;字符串>;每个基本数据类型的转换,java,json,jackson,Java,Json,Jackson,此代码的目的是将未知数量的参数和未知数量的值转换为映射(键-参数名称,值-可能的参数值列表),假设每种类型都将解析为字符串 问题是数字和其他格式没有解析成字符串,而且jsonField.getValue()不是值节点(jsonField.getValue().isValueNode()返回false),所以我不能使用jsonField.getValue().asText(),因为它返回null 我的做法: import com.fasterxml.jackson.databind.JsonNod
映射
(键-参数名称,值-可能的参数值列表),假设每种类型都将解析为字符串
问题是数字和其他格式没有解析成字符串,而且jsonField.getValue()
不是值节点(jsonField.getValue().isValueNode()
返回false
),所以我不能使用jsonField.getValue().asText()
,因为它返回null
我的做法:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.map.ListOrderedMap;
public ListOrderedMap<String, ArrayList<String>> convertParamsFromJson(String jsonParams) throws IOException {
ListOrderedMap<String, ArrayList<String>> convertedParams = new ListOrderedMap<>();
Iterator<Entry<String, JsonNode>> fieldsFromJson = convertFromJson(jsonParams).fields();
ObjectMapper mapper = new ObjectMapper();
while (fieldsFromJson.hasNext()) {
Entry<String, JsonNode> jsonField = fieldsFromJson.next();
String paramName = jsonField.getKey();
String paramValue = jsonField.getValue();
if (jsonField.getValue().isArray()) {
convertedParams.put(paramName, mapper.convertValue(paramValue, ArrayList.class));
}
}
return convertedParams;
}
预期产出:
<[MapEntry[key="firstparam", value=["1"]],
MapEntry[key="secondparam", value=["a","b","c","d"]],
MapEntry[key="thirdparam", value=["1","2","3"]],
MapEntry[key="fourthparam", value=["true", "false"]]]>
输出:
<[MapEntry[key="firstparam", value=[1]],
MapEntry[key="secondparam", value=["a", "b", "c", "d"]],
MapEntry[key="thirdparam", value=[1,2,3]],
MapEntry[key="fourthparam", value=[true, false]]]>
默认情况下
Jackson
库将JSON
原语转换为合适的类型。考虑下面的例子:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
String json = "{\"firstParam\":[1],\"secondParam\": [\"a\",\"b\",\"c\",\"d\"],\"thirdParam\": [1,2,3],\"fourthParam\":[true,false]}";
ObjectMapper mapper = new ObjectMapper();
Map<String, List<Object>> map = mapper.readValue(json, Map.class);
map.forEach((k, v) -> {
System.out.print(k + " => ");
v.forEach(i -> System.out.print(i + " (" + i.getClass().getSimpleName() + "), "));
System.out.println();
});
}
}
但是,我们可以通过使用TypeReference
提供需要实现的类型,强制将列表项转换为String
:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
String json = "{\"firstParam\":[1],\"secondParam\": [\"a\",\"b\",\"c\",\"d\"],\"thirdParam\": [1,2,3],\"fourthParam\":[true,false]}";
TypeReference<LinkedHashMap<String, List<String>>> mapOfStringListsType = new TypeReference<LinkedHashMap<String, List<String>>>() {};
ObjectMapper mapper = new ObjectMapper();
Map<String, List<String>> map = mapper.readValue(json, mapOfStringListsType);
map.forEach((k, v) -> {
System.out.print(k + " => ");
v.forEach(i -> System.out.print(i + " (" + i.getClass().getSimpleName() + "), "));
System.out.println();
});
}
}
简要介绍一下jackson文档,或者使用自定义的
ObjectMapper
,或者将默认值设置为使用自定义反序列化规则,以便始终映射到字符串,即使json具有不同的类型。我不知道这是如何实现的。
firstParam => 1 (Integer),
secondParam => a (String), b (String), c (String), d (String),
thirdParam => 1 (Integer), 2 (Integer), 3 (Integer),
fourthParam => true (Boolean), false (Boolean),
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JsonApp {
public static void main(String[] args) throws Exception {
String json = "{\"firstParam\":[1],\"secondParam\": [\"a\",\"b\",\"c\",\"d\"],\"thirdParam\": [1,2,3],\"fourthParam\":[true,false]}";
TypeReference<LinkedHashMap<String, List<String>>> mapOfStringListsType = new TypeReference<LinkedHashMap<String, List<String>>>() {};
ObjectMapper mapper = new ObjectMapper();
Map<String, List<String>> map = mapper.readValue(json, mapOfStringListsType);
map.forEach((k, v) -> {
System.out.print(k + " => ");
v.forEach(i -> System.out.print(i + " (" + i.getClass().getSimpleName() + "), "));
System.out.println();
});
}
}
firstParam => 1 (String),
secondParam => a (String), b (String), c (String), d (String),
thirdParam => 1 (String), 2 (String), 3 (String),
fourthParam => true (String), false (String),