我有一个JSON对象,如下所示:
[{"var1":"value1","var2":"value2"},{"var2":"value22","var3":[["0","1","2"],["3","4","5"],["6","7","8"]]}]
(注意:var2
在示例中出现两次,复数形式为var3
。)
所需的输出应该是地图对象,如:
key value
var1 value1
var2 value2,value22
var3 [["0","1","2"],["3","4","5"],["6","7","8"]]
我想要的是将其转换为地图对象,其中第一个元素(var1
,var2
,var3
)作为关键字,相应的值作为地图中的值。如果使用相同的密钥(例如:var2
),则属于该密钥的两个值应该连接,但是例如用逗号分隔。
有人可以帮我这个吗?
答案 0 :(得分:1)
您可以使用Jackson转换为JSON和Map。使用以下代码并实例化JSonAdapter类,使用方法marshal(String)
将json字符串转换为map,将unmarshall(Map)
转换为import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonAdapter {
private static final ObjectMapper MAPPER = new ObjectMapper();
public String unmarshal(final Map<?, ?> jsonList) throws Exception {
return MAPPER.writeValueAsString(jsonList);
}
public Map<?, ?> marshal(final String jsonString) throws Exception {
try {
return MAPPER.readValue(jsonString, new TypeReference<Map<?, ?>>() {
});
} catch (final IOException e) {
e.printStackTrace();
}
return null;
}
}
,反之亦然。
List
答案 1 :(得分:1)
您不需要适配器来解析json。你只需要告诉ObjectMapper究竟要解析的类型。你还需要一些后处理,因为你想要一些关于重复键的特殊处理
你从GIT获得杰克逊:https://github.com/FasterXML/jackson
这是一个完整的解决方案:
import java.util.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.type.TypeFactory;
public class Test
{
public static void main(String[] args)
{
String input = "[{\"var1\":\"value1\",\"var2\":\"value2\"},{\"var2\":\"value22\",\"var3\":[[\"0\",\"1\",\"2\"],[\"3\",\"4\",\"5\"],[\"6\",\"7\",\"8\"]]}]" ;
Map<String, String> result = new HashMap<>(); // final result, with duplicate keys handles and everything
try {
// ObjectMapper is Jackson json parser
ObjectMapper om = new ObjectMapper();
// we need to tell ObjectMapper what type to parse into
// in this case: list of maps where key is string and value is some cimplex Object
TypeFactory tf = om.getTypeFactory();
JavaType mapType = tf.constructMapType(HashMap.class, String.class, Object.class);
JavaType listType = tf.constructCollectionType(List.class, mapType);
@SuppressWarnings("unchecked")
// finally we parse the input into the data struct
List<Map<String, Object>> list = (List<Map<String, Object>>)om.readValue(input, listType);
// post procesing: populate result, taking care of duplicates
for (Map<String, Object> listItem : list) {
for (Map.Entry<String, Object> mapItem : listItem.entrySet()) {
String key = mapItem.getKey();
String value = mapItem.getValue().toString();
if (result.containsKey(key)) value = result.get(key) + "," + value;
result.put(key, value);
}
}
// result sohuld hold expected outut now
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
{var3=[[0, 1, 2], [3, 4, 5], [6, 7, 8]], var2=value2,value22, var1=value1}
答案 2 :(得分:0)
没有预定义的API来执行您的操作,您必须编写自己的解析器以将JSONObject
转换为Map
。
请参阅下面的一些提示:
public static Map<String, Object> toMap(JSONObject object) throws JSONException{
Map<String, List<String>> map = new HashMap<String, List<String>>();
Iterator<String> keysIterator = object.keys();
while(keysIterator .hasNext()) {
String key = keysIterator.next();
Object value = object.get(key);
if(map.contains(key)){
... //do your task
}
... //get value of the key
map.put(key, yourValue)
}