public static void main(String[] args) { Map map=new HashMap(); map.put("我","妹"); map.put("擦","哇"); map.put("你","呀"); JSONObject json = JSONObject.fromObject(map); System.out.println(json); } 輸出的結果 {"我":"妹","擦":"哇","你":"呀"} toBean(); 首先一个javabean对象 public class Student { private int id ; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return this.id + ", " + this.name + ", " + this.age; } } 然后测试toBean方法的类 import net.sf.json.JSONObject; public class ToBeanTest { public static void main(String[] args) { String json = "{id:'1001',name:'张三',age:'22'}"; Student stu = new Student(); JSONObject obj = JSONObject.fromObject(json); stu = (Student)JSONObject.toBean(obj, Student.class); System.out.println(stu); } } 输出结果为1001, 张三, 22 然后我们在修改修改 import net.sf.json.JSONObject; public class ToBeanTest { public static void main(String[] args) { String json = "{id:'1001',name:'张三'}"; Student stu = new Student(); JSONObject obj = JSONObject.fromObject(json); stu = (Student)JSONObject.toBean(obj, Student.class); System.out.println(stu); } } 把年龄给去掉age为int型,输出结果为:1001, 张三, 0 然后再做小小改动 import net.sf.json.JSONObject; public class ToBeanTest { public static void main(String[] args) { String json = "{id:'1001',age:'22'}"; Student stu = new Student(); JSONObject obj = JSONObject.fromObject(json); stu = (Student)JSONObject.toBean(obj, Student.class); System.out.println(stu); } } 把姓名给去掉name为String型,输出结果为:1001, null, 22 再改动一下: import net.sf.json.JSONObject; public class ToBeanTest { public static void main(String[] args) { String json = "{id:'1001',name:'张三',age:'nn'}"; Student stu = new Student(); JSONObject obj = JSONObject.fromObject(json); stu = (Student)JSONObject.toBean(obj, Student.class); System.out.println(stu); } } 把age改成非整形,输出结果为: 1001, 张三, 0 再改动一下: import net.sf.json.JSONObject; public class ToBeanTest { public static void main(String[] args) { String json = "{id:'1001',name:'张三',age:'22',sex:'男'}"; Student stu = new Student(); JSONObject obj = JSONObject.fromObject(json); stu = (Student)JSONObject.toBean(obj, Student.class); System.out.println(stu); } } 加了一个sex:'男'的一对键值,输出结果为: 1001, 张三, 22
JSONObject与JSONArray的使用方法 一、JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar 二、JSONObject对象使用 JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包。在本例中,我们将使用JSONObject类创建JSONObject对象,然后我们打印这些对象的值。为了使用JSONObject对象,我们要引入"net.sf.json"包。为了给对象添加元素,我们要使用put()方法。 package com.tjcyjd; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JSONObjectSample { // 创建JSONObject对象 private static JSONObject createJSONObject() { JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "yangjinde"); jsonObject.put("sex", "男"); jsonObject.put("QQ", "908599713"); jsonObject.put("yjd.score", new Integer(100)); jsonObject.put("nickname", "搁浅"); return jsonObject; } public static void main(String[] args) { JSONObject jsonObject = JSONObjectSample.createJSONObject(); // 输出jsonobject对象 System.out.println("jsonObject:" + jsonObject); // 判读输出对象的类型 boolean isArray = jsonObject.isArray(); boolean isEmpty = jsonObject.isEmpty(); boolean isNullObject = jsonObject.isNullObject(); System.out.println("isArray:" + isArray + " isEmpty:" + isEmpty + " isNullObject:" + isNullObject); // 往JSONObject添加属性 jsonObject.element("address", "北京中关村"); System.out.println("添加属性后的对象:" + jsonObject); // 创建一个JSONArray对象 JSONArray jsonArray = new JSONArray(); jsonArray.add(0, "this is a jsonArray value"); jsonArray.add(1, "this is another jsonArray value"); // 往JSONObject添加JSONArray jsonObject.element("jsonArray", jsonArray); JSONArray array = jsonObject.getJSONArray("jsonArray"); System.out.println("array:" + array); System.out.println("添加JSONArray后的对象:" + jsonObject); // 根据key返回一个字符串 String username = jsonObject.getString("username"); System.out.println("username:" + username); // 把字符转换为 JSONObject String temp = jsonObject.toString(); System.out.println("要转换为JSONObject的字符串为:" + temp); JSONObject object = JSONObject.fromObject(temp); // 转换后根据Key返回值 System.out.println("qq:" + object.get("QQ")); } } 运行的结果如下: jsonObject:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅"} isArray:false isEmpty:false isNullObject:false 添加属性后的对象:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅","address":"北京中关村"} array:["this is a jsonArray value","this is another jsonArray value"] 添加JSONArray后的对象:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅","address":"北京中关村","jsonArray":["this is a jsonArray value","this is another jsonArray value"]} username:yangjinde 要转换为JSONObject的字符串为:{"username":"yangjinde","sex":"男","QQ":"908599713","yjd.score":100,"nickname":"搁浅","address":"北京中关村","jsonArray":["this is a jsonArray value","this is another jsonArray value"]} qq:908599713
对象的toString方法:
@Override
public String toString() {
JSONObject jsonObj = JSONObject.fromObject(this);
return jsonObj.toString();
}
其他地方用对象.toString()就是json