jackson 怎么把json转为map
1个回答
展开全部
package com.test;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/**
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String s = "{ \"name\" : \"萧远山\", \"sex\" : \"男\", \"age\" : \"23\",\"address\" : \"河南郑州\"}";
try {
Map<?, ?> map = mapper.readValue(s, Map.class);
System.out.println(map.size());
Iterator<?> iterator = map.keySet().iterator();
while ( iterator.hasNext() ) {
Object key = iterator.next();
System.out.print(key+":");
System.out.println(map.get(key).toString());
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/**
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String s = "{ \"name\" : \"萧远山\", \"sex\" : \"男\", \"age\" : \"23\",\"address\" : \"河南郑州\"}";
try {
Map<?, ?> map = mapper.readValue(s, Map.class);
System.out.println(map.size());
Iterator<?> iterator = map.keySet().iterator();
while ( iterator.hasNext() ) {
Object key = iterator.next();
System.out.print(key+":");
System.out.println(map.get(key).toString());
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询