gpt4 book ai didi

java - 每个原始数据类型的 JSON 到 Map 转换

转载 作者:行者123 更新时间:2023-11-30 05:38:40 25 4
gpt4 key购买 nike

此代码的目的是将未知数量的具有未知数量值的参数转换为 Map<String, List<String>> (key - 参数名称,value - 可能的参数值列表),假设每种类型都会解析为 String。

问题在于数字和其他格式没有解析为字符串,并且 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;
}

输入:

{
"firstParam":[1],
"secondParam": ["a","b","c","d"],
"thirdParam": [1,2,3],
"fourthParam":[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"]]]>

输出:

  <[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();
});
}
}

上面的代码打印:

firstParam => 1 (Integer), 
secondParam => a (String), b (String), c (String), d (String),
thirdParam => 1 (Integer), 2 (Integer), 3 (Integer),
fourthParam => true (Boolean), false (Boolean),

但是我们可以通过使用 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();
});
}
}

上面的代码打印:

firstParam => 1 (String), 
secondParam => a (String), b (String), c (String), d (String),
thirdParam => 1 (String), 2 (String), 3 (String),
fourthParam => true (String), false (String),

关于java - 每个原始数据类型的 JSON 到 Map<String> 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com