关于javaWeb开发中json数据的处理总计

时间:2022-12-23 13:10:39

关于javaWeb开发中json数据的处理总结

前言:java中常见的json解析类库有Gson、json-lib、jackson 一、jackson进行JSON解析和序列化:(依赖包下载地址http://wiki.fasterxml.com/JacksonDownload) (1)所需包: jackson-core-2.2.3.jar(核心jar包,下载地址 jackson-annotations-2.2.3.jar(该包提供Json注解支持,下载地址
jackson-databind-2.2.3.jar(下载地址 (2)序列化User类:使用writeValueAsString()
        User user = new User();  
user.setName("小民");
user.setEmail("xiaomin@sina.com");
user.setAge(20);

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));

/**
* ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
* ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
* writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
* writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
* writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
* writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
*/
ObjectMapper mapper = new ObjectMapper();

//User类转JSON
//输出结果:{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);

//Java集合转JSON          //输出结果:[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]          List<User> users = new ArrayList<User>();          users.add(user);          String jsonlist = mapper.writeValueAsString(users);          System.out.println(jsonlist); 
-----------------------------------------------------------------------------------------------------------------
/**   * 将map转换成json字符串 */ public static String writeMapJSON(Map<String, Object> map) { String jsonString ="";ObjectMapper objectMapper = new ObjectMapper();try {  jsonString = objectMapper.writeValueAsString(map); } catch (IOException e) {  e.printStackTrace();  }  return jsonString;} 
/**   * 将list<Map>转换成json字符串   */ public static String writeListMapJSON(List<Map<String,String>> listMap) {String jsonString ="";ObjectMapper objectMapper = new ObjectMapper();try {  jsonString = objectMapper.writeValueAsString(listMap);  } catch (IOException e) {  e.printStackTrace();  } return jsonString;} 

(2)JSON转Java类[JSON反序列化]使用readValue();
   public static void main(String[] args) throws ParseException, IOException {  
String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";

/**
* ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
*/
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);
}
/** * 将数组对象转换成json字符串 */public static String writeArrayJSON(String[] array) {String jsonString ="";ObjectMapper objectMapper = new ObjectMapper();    try {    jsonString = objectMapper.writeValueAsString(array);        } catch (IOException e) {        e.printStackTrace();    }    return jsonString;}
/**   * json字符串转换成Map   */ @SuppressWarnings("unchecked")public static Map<String,Object> readJsonMapObject(String jsonString){ObjectMapper objectMapper = new ObjectMapper();Map<String,Object> map = new LinkedHashMap<String,Object>();try{map = objectMapper.readValue(jsonString, Map.class);  }catch (JsonParseException e) {  e.printStackTrace();  } catch (JsonMappingException e) {  e.printStackTrace();  } catch (IOException e) {  e.printStackTrace();  }  return map;}
/**   * json字符串转换成list   */ @SuppressWarnings("unchecked")public static List<String> readJsonList(String jsonString){ObjectMapper objectMapper = new ObjectMapper();List<String> list = new ArrayList<String>();try{list = objectMapper.readValue(jsonString, List.class);  }catch (JsonParseException e) {  e.printStackTrace();  } catch (JsonMappingException e) {  e.printStackTrace();  } catch (IOException e) {  e.printStackTrace();  }  return list;}
/**   * json字符串转换成list<map>   */ @SuppressWarnings("unchecked")public static List<Map<String, String>> readJsonListMap(String jsonString){ObjectMapper objectMapper = new ObjectMapper();List<Map<String, String>> list = new ArrayList<Map<String, String>>();try{list = objectMapper.readValue(jsonString, List.class);  }catch (JsonParseException e) {  e.printStackTrace();  } catch (JsonMappingException e) {  e.printStackTrace();  } catch (IOException e) {  e.printStackTrace();  }  return list;}public static List<Map<String, Object>> readJsonListMapObj(String jsonString){ObjectMapper objectMapper = new ObjectMapper();List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();try{list = objectMapper.readValue(jsonString, List.class);  }catch (JsonParseException e) {  e.printStackTrace();  } catch (JsonMappingException e) {  e.printStackTrace();  } catch (IOException e) {  e.printStackTrace();  }  return list;}
/** * json字符串转换成map<map>   * @param jsonString * @return */public static Map<String, Map<String, Object>> readJsonToMapObj(String jsonString){ObjectMapper objectMapper = new ObjectMapper();Map<String, Map<String, Object>> list = null;try{list = objectMapper.readValue(jsonString,Map.class); }catch (JsonParseException e) {  e.printStackTrace();  } catch (JsonMappingException e) {  e.printStackTrace();  } catch (IOException e) {  e.printStackTrace();  }  return list;}

二、json-lib进行JSON解析:

(1)使用JSONArray.fromObject()函数:http://www.cnblogs.com/sunzn/archive/2013/02/12/2910241.html

二、Gson进行JSON解析:(1)使用toJson()方法:
public static String getJson(Object list) {
Gson gson = new Gson();
return gson.toJson(list);
}

示例:http://blog.csdn.net/axuanqq/article/details/51441590