我正在尝试做这样的事情,但它不起作用:
Map<String, String> propertyMap = new HashMap<String, String>();
propertyMap = JacksonUtils.fromJSON(properties, Map.class);
但IDE说:
未经核实的作业
Map to Map<String,String>
这样做的正确方法是什么? 我只使用Jackson,因为这是项目中已有的,是否有一种原生的Java方式转换为JSON或从JSON转换?
在PHP中我只是json_decode($str)
而且我会回到一个数组。我在这里需要基本相同的东西。
答案 0 :(得分:273)
我有以下代码:
public void testJackson() throws IOException {
ObjectMapper mapper = new ObjectMapper();
File from = new File("albumnList.txt");
TypeReference<HashMap<String,Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> o = mapper.readValue(from, typeRef);
System.out.println("Got " + o);
}
它正在读取文件,但mapper.readValue()
也会接受InputStream
,您可以使用以下内容从字符串中获取InputStream
:
new ByteArrayInputStream(astring.getBytes("UTF-8"));
关于my blog上的映射器有更多解释。
答案 1 :(得分:50)
试试TypeFactory
。这是Jackson JSON(2.8.4)的代码。
Map<String, String> result;
ObjectMapper mapper;
TypeFactory factory;
MapType type;
factory = TypeFactory.defaultInstance();
type = factory.constructMapType(HashMap.class, String.class, String.class);
mapper = new ObjectMapper();
result = mapper.readValue(data, type);
以下是旧版Jackson JSON的代码。
Map<String, String> result = new ObjectMapper().readValue(
data, TypeFactory.mapType(HashMap.class, String.class, String.class));
答案 2 :(得分:23)
警告你得到的是编译器,而不是库(或实用程序方法)。
直接使用杰克逊的最简单方法是:
HashMap<String,Object> props;
// src is a File, InputStream, String or such
props = new ObjectMapper().readValue(src, new TypeReference<HashMap<String,Object>>() {});
// or:
props = (HashMap<String,Object>) new ObjectMapper().readValue(src, HashMap.class);
// or even just:
@SuppressWarnings("unchecked") // suppresses typed/untype mismatch warnings, which is harmless
props = new ObjectMapper().readValue(src, HashMap.class);
你调用的实用方法可能只是做了类似的事情。
答案 3 :(得分:17)
ObjectReader reader = new ObjectMapper().readerFor(Map.class);
Map<String, String> map = reader.readValue("{\"foo\":\"val\"}");
请注意,reader
实例是线程安全的。
答案 4 :(得分:9)
从String转换为JSON Map:
Map<String,String> map = new HashMap<String,String>();
ObjectMapper mapper = new ObjectMapper();
map = mapper.readValue(string, HashMap.class);
答案 5 :(得分:5)
JavaType javaType = objectMapper.getTypeFactory().constructParameterizedType(Map.class, Key.class, Value.class);
Map<Key, Value> map=objectMapper.readValue(jsonStr, javaType);
我认为这可以解决您的问题。
答案 6 :(得分:3)
以下适用于我:
Map<String, String> propertyMap = getJsonAsMap(json);
其中getJsonAsMap
的定义如下:
public HashMap<String, String> getJsonAsMap(String json)
{
try
{
ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
HashMap<String, String> result = mapper.readValue(json, typeRef);
return result;
}
catch (Exception e)
{
throw new RuntimeException("Couldnt parse json:" + json, e);
}
}
请注意,如果你的json中有子对象,那么将失败(因为它们不是String
,它们是另一个HashMap
),但是会有效如果你的json是像这样的属性的键值列表:
{
"client_id": "my super id",
"exp": 1481918304,
"iat": "1450382274",
"url": "http://www.example.com"
}
答案 7 :(得分:1)
为什么不使用here中提到的Google的Gson?
很直接,为我做了这份工作:
<table id="example" style="color: black;" class="display compact cell-border" cellspacing="0">
<thead>
<tr>
<th rowspan="2">Sl.No</th>
<th rowspan="2">Zone</th>
<th colspan="2">Allotted</th>
<th colspan="2">Vacant</th>
<th colspan="2">Amenities</th>
<th colspan="2">Total</th>
</tr>
<tr>
<th>No Of Plots</th>
<th>Area</th>
<th>No Of Plots</th>
<th>Area</th>
<th>No Of Plots</th>
<th>Area</th>
<th>No Of Plots</th>
<th>Area</th>
</tr>
</thead>
</table>
答案 8 :(得分:1)
以下是此问题的通用解决方案。
public static <K extends Object, V extends Object> Map<K, V> getJsonAsMap(String json, K key, V value) {
try {
ObjectMapper mapper = new ObjectMapper();
TypeReference<Map<K, V>> typeRef = new TypeReference<Map<K, V>>() {
};
return mapper.readValue(json, typeRef);
} catch (Exception e) {
throw new RuntimeException("Couldnt parse json:" + json, e);
}
}
希望有一天有人会想创建一个util方法来转换为任何Key / value类型的Map,因此这个答案:)
答案 9 :(得分:0)
只想给科特林一个答案
val propertyMap = objectMapper.readValue<Map<String,String>>(properties, object : TypeReference<Map<String, String>>() {})