json学习系列(2)-生成JSONObject的方法

生成JSONObject一般有两种方式,通过javabean或者map类型来生成。如下面的例子:

先定义一个User实体类:

package com.pcitc.json;

/**
* 用户实体类
*
* @Description
* @author admin
* @created 2015年6月29日 下午3:43:15
* @version
* @history
* @see
*/
public class User {
public String username;
public String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

测试类如下:

package com.pcitc.json;

import java.util.HashMap;
import net.sf.json.JSONObject; /**
* 测试类
*
* @Description
* @author admin
* @created 2015年6月29日 下午3:57:01
* @version
* @history
* @see
*/
public class Test { public static void main(String args[]) { User user = new User();
user.setUsername("root");
user.setPassword("1234");
// 通过javabean来生成JSONObject对象
JSONObject json1 = JSONObject.fromObject(user);
System.out.println("javabean方式:\r\n" + json1.toString());
HashMap<Object, Object> userMap = new HashMap<Object, Object>();
userMap.put("username", "root");
userMap.put("password", "1234");
// 通过map类型来生成JSONObject
JSONObject json2 = JSONObject.fromObject(userMap);
System.out.println("map方式:\r\n" + json2.toString()); }
}

测试结果如下所示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbAAAABvCAIAAAACMwIzAAAKaUlEQVR4nO2d28GrKhBGbWtbEO38lnCqsBmL8TyEGC4zA3iNZq23iJDh9smAQjfDWYyu67quc+Pp/zz9/RP/Vrt+Gb6I/v1Nm9LYmKfp79+Swug2mnM227Nvpfz39+8LisM3k4VVGZYLqttuHXw9txHEb2H6+/fubNd3/y/huuf5mSCIAAAeWRD/AwD4PRghAgB4EEQAAA+CCADgQRABADwIIgCAp14QR9d1XT8c91LW6B7/khMAfDW/Lohd1zBGft3cFAUAbsQX9e2rRoj1ArfciSYCPJIv6tinCWJXwoh4gnkAcBWFHp5+RZ24zGHwEjQNfaJso4tiirHegjgNvfzJdhgr0U09wX6YgsBUbm3tqxfNl83MfwLcnYYhTyJr8zT0Hw0YXSAJ8Z2xQNqxPj+jWFkSKxJMzZ/bR3za/Q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Alia6FbgqinFbcHaXuxdz2GYbGaxa6FdsiBeTSCZUaJNYJo5Ss2450xI+gdfIZjax0UoR8vYZmnBQnXP73aKg29eJN6bSqzgwQxd5kDoU8U7GPvitqWVwRgnueiIMb7HNZSEkQxwezBlW45GwQG99qjUdmq0j6Pqhm7o+Ur1+GaoBMMTmxPJ8rk/61rAHrQMtMWy6AbS6WhNpt623fEFMRlcVgk1/mwZ6yw/MmvDGyiZoTYvC5VECn5wZc4MLEbU9nZxMmgPD1LEE0zdkfLl+DWhBKgjhesNPdGEpVoVkItQn3ko7cN/yKJc64fpuVKoTSs4v0mQawYd2hivsJyfGaDC+YQ43Sjp7las2sEUT/koCSI5z04q3vsrUaIesknGAurUdBriPjyw0fn5zqWMaM9QlQV6FsEsW6wZj+xrdGl9H/oocYhgqjP8VkJty2yKSGpICazkGVB3PSe1qo5xBr3MTLKCBIvNNjSMrmUj1Si1R2x5FOMlhUFja7rnevfVeWcs2ZUK+YrA3Pfg9JL5hAr09Kn/VhU2ZdDBDF6jLlxeZzPmSuVpJo6WpUtWxuNhsmFhxyYgmiZUWJHQZyTsUB8lxGU5bu63hoV0Vqk1UrebADFoOgZm1S6aIdVvJ9I4YizhtZaFlb/Ey2WCjIu3hW+k2UOgqhxkCDCHbEn9qW7mZW/H/Rmiy2f7sGjaB34IIi3ZMuU0A+wZXMHeAyt84fvOHSsOxG/lgkSbP8FAOBBEAEAPAgiAIAHQQQA8CCIAAAeBBEAwIMgAgB4EEQAAA+CCADgQRABADwIIgCAB0EEAPAgiAAAHgQRAMCDIAIAeBBEAAAPgggA4EEQAQA8CCIAgAdBBADwIIgAAB4EEQDAgyACAHgQRAAAD4IIAOBBEAEAPAgiAIAHQQQA8CCIAAAeBBEAwIMgAgB4/gewSThqU5T21AAAAABJRU5ErkJggg==" alt="" />

接下来从源码层次分析一下JSONObject.fromObject()方法:

public static JSONObject fromObject(Object object)
{
return fromObject(object, new JsonConfig());
}

此函数可以接受的参数类型为:JSON formatted strings,Maps,DynaBeans and JavaBeans。
【注意】DynaBeans是commons-beanutils定义的动态bean。DynaBean并不是Java中所定义的Bean,而是一种"假"的Bean。因为它并不是通过getXXX和setXXX方法,对XXX属性进行取值和设值的。
如果object是其他类型的参数呢?比如说数字,逻辑值,非json格式的字符串,那么将生产空的JSONObject对象。

if (JSONUtils.isNumber(object) || JSONUtils.isBoolean(object) || JSONUtils.isString(object))
{
return new JSONObject();
}

JSONObject的构造函数有两个:

public JSONObject()
{
this.properties = new ListOrderedMap();
}
public JSONObject(boolean isNull)
{
this();
this.nullObject = isNull;
}

不过,通常都是使用第一个无参的构造函数,第二个构造函数使用情况很少。

上一篇:Python爬虫:抓取新浪新闻数据


下一篇:Python爬虫:抓取手机APP的数据