JSONObject 转 JSON 字符串
JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "wjw"); jsonObject.put("age", 22);
jsonObject.put("sex", "男");
jsonObject.put("school", "商职");
String jsonStr = JSONObject.toJSONString(jsonObject);
System.out.println(jsonStr);
--------------------------结果-----------------
{"school":"商职","sex":"男","name":"wjw","age":22}
JSON 字符串 转 JSONObject 对象
String jsonStr = "{\"school\":\"商职\",\"sex\":\"男\",\"name\":\"wjw\",\"age\":22}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); System.out.println(jsonObject.getString("name")); System.out.println(jsonObject.getInteger("age"));
--------------------------结果-----------------
wjw
22