Json中对象嵌套对象

Result result=new Result();
		Customer cu=new Customer();
		cu.setEmail("[email protected]");
		cu.setNick_name("王鹏");
		result.setCustomer(cu);
		net.sf.json.JSONObject fromObject = net.sf.json.JSONObject.fromObject(result); //json-lib   将对象转为json
		System.out.println(fromObject);
		
		String str = JSON.toJSONString(result);//fastjson   将对象转为json字符串  {"customer":{"email":"[email protected]","nick_name":"王鹏"}} {对象里面套对象}
		System.out.println(str);

打印结果为:

{"customer":{"nick_name":"王鹏","email":"[email protected]"}}
{"customer":{"email":"[email protected]","nick_name":"王鹏"}}

你可能感兴趣的:(Json中对象嵌套对象)