日期:2014-05-16 浏览次数:20414 次
需要基本的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
?
package test.json;
import net.sf.json.JSONObject;
public class JsonTest {
?public static void main(String[] args) {
??JSONObject jsonObject = new JSONObject();
??
??jsonObject.put("name" ,? "kevin" );??
??jsonObject.put("Max.score" ,? new? Integer( 100 ));??
??jsonObject.put("nickname" ,? "picglet" ); // 这里是要被下面一行代码的效果覆盖的
??jsonObject.put("nickname" ,? "picglet123" );
??jsonObject.put("children" , buildSubJson() );
??
??System.out.println(jsonObject.toString());
?}
?
?public static String buildSubJson(){
??JSONObject jsonObject = new JSONObject();
??jsonObject.put("sub1" ,? "kevin" );??
??jsonObject.put("sub2" ,? new? Integer( 100 ));??
??
??return jsonObject.toString();
?}
}
?
结果 :
{"nickname":"picglet123","Max.score":100,"Min.score":50,"name":"kevin","children":{"sub2":100,"sub1":"kevin"}}