ObjectMapper

司寇研
2023-12-01

转载地址:https://blog.csdn.net/u013065023/article/details/54970298
public static void main(String[] args) {
		ObjectMapper mapper = new ObjectMapper();
		String jsonStr = "{\"name\":\"Mahesh\", \"age\":21}";
		try {
			// map json to student
			Student s = mapper.readValue(jsonStr, Student.class);
			System.out.println(s);// Student [name=Mahesh, age=21]
			// student to json
			jsonStr = mapper.writeValueAsString(s);
			System.out.println(jsonStr);// {"name":"Mahesh","age":21}
		} catch (JsonParseException e) {
	         e.printStackTrace();
	      } catch (JsonMappingException e) {
	         e.printStackTrace();
	      } catch (IOException e) {
	         e.printStackTrace();
		}
	}

 类似资料: