public static void JacksonTest() {//推薦 //{"MNG001":[{"ID":"1","PWD":"2"}],"MNG002":[{"FaultCode":"1","PWD":"3"}]} String json = "{\"MNG001\":[{\"ID\":\"1\",\"PWD\":\"2\"}],\"MNG002\":[{\"FaultCode\":\"1\",\"PWD\":\"3\"}]}"; ObjectMapper mapper = new ObjectMapper(); //把json字符串轉為對象 Object object = null; try { object = mapper.readValue(json, Object.class); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("JsonToObject-->"+object.toString()); //把對象轉為json字符串 String objectjson = ""; try { objectjson = mapper.writeValueAsString(object); } catch (JsonProcessingException e) { e.printStackTrace(); } System.out.println("ObjectToJson-->"+objectjson); } public static void GsonTest() { //{"MNG001":[{"ID":"1","PWD":"2"}],"MNG002":[{"FaultCode":"1","PWD":"3"}]} String json = "{\"MNG001\":[{\"ID\":\"1\",\"PWD\":\"2\"}],\"MNG002\":[{\"FaultCode\":\"1\",\"PWD\":\"3\"}]}"; Gson gson = new Gson(); //把json字符串轉對象 Object object = gson.fromJson(json, Object.class); System.out.println("JsonToObject-->"+object.toString()); //把對象轉json字符串 String objectjson = gson.toJson(object); System.out.println("ObjectToJson-->"+objectjson); }