PackageInfo.java
package test_2013_4_17;
/**
*
* @ClassName Packages
*
* @Project egame.server.open.biz
*
* @Author yuchao
*
* @Create Date 2013-4-17
*
*/
public class PackageInfo {
private String package_name;
private String version;
/**
* @return the package_name
*/
public String getPackage_name() {
return package_name;
}
/**
* @param package_name the package_name to set
*/
public void setPackage_name(String package_name) {
this.package_name = package_name;
}
/**
* @return the version
*/
public String getVersion() {
return version;
}
/**
* @param version the version to set
*/
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "PackageInfo [package_name=" + package_name + ", version="
+ version +"]";
}
}
TestJson.java
package test_2013_4_17;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
public class TestJson {
public static void main(String[] args) throws Exception {
testJsonToObject();
testJsonToList();
testJsonToMap();
testJsonMapInList();
}
/**
* json字符串转换成普通对象
*/
private static void testJsonToObject() throws Exception{
String jsonStr = "{'package_name':'愤怒的小鸟','version':'1.01'}";
ObjectMapper mapper = new ObjectMapper();
//允许键值对的引号为单引号,否则只能为双引号
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
PackageInfo packageInfo = mapper.readValue(jsonStr, PackageInfo.class);
System.out.println(packageInfo);
}
/**
* json字符串转换成List
*/
private static void testJsonToList() throws Exception{
String jsonStr = "[{'package_name':'愤怒的小鸟','version':'1.01'}" +
",{'package_name':'愤怒的小鸟','version':'1.01'}]";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// List<PackageInfo> packageInfos = mapper.readValue(jsonStr, List.class);
List<PackageInfo> packageInfos = mapper.readValue(jsonStr,new TypeReference<List<PackageInfo>>(){ });
System.out.println(packageInfos);
}
/**
* json字符串转换成Map
*/
private static void testJsonToMap() throws Exception{
String jsonStr = "{'xiaoniao':'1.01','360':'2.2'}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
// Map map = mapper.readValue(jsonStr, Map.class);
Map<String,String> map = mapper.readValue(jsonStr,new TypeReference<Map<String,String>>(){ });
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = (String) it.next();
System.out.println("key:"+str+", "+"value:"+map.get(str));
}
}
/**
* 转换这种格式的json字符串到一个List<Map>对象
* [{'com.tencent.mm.apk':'0'},{'com.qihoo360.mobilesafe.apk':'1'}]
*/
private static void testJsonMapInList() throws Exception{
String jsonStr = "[{'xiaoniao':'1.01'},{'360':'2.2'}]";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//下面两种都可以
// List<Map<String,String>> list = mapper.readValue(jsonStr, List.class);
List<Map<String,String>> list = mapper.readValue(jsonStr,new TypeReference<List<Map<String,String>>>(){ });
for(Map<String, String> map : list){
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = (String) it.next();
System.out.println("key:"+str+", "+"value:"+map.get(str));
}
}
}
}