JSON的三种构建

1、定义函数

private static void JsonObject(){

          JsonObject wangxiaoer = new JsonObject();
          Object nullobject = null;
          try{

                   wangxiaoer.put("name","王小二");

                   wangxiaoer.put("age",12);

                   wangxiaoer.put("birthday","1990-1-1");

                   wangxiaoer.put("school","蓝翔");

                   wangxiaoer.put("major",new String{}["理发","挖掘机"]);

                   wangxiaoer.put("has_girfrienf",false);

                   wangxiaoer.put("house",nullobject);

                   wangxiaoer.put("comment","这是注释");

                   System.out.printh(wangxiaoer.toString());
          }catch(JsonExpection e){

               e.printStackTrace();

          }

}

2、使用Map创建

        private static void CreatJsonByMap(){

             Map<string,object> wangxiaoer = new HasMap<string,object>;

             Object nullobject = null;

             wangxiaoer.put("name","王小二");

             wangxiaoer.put("age",12);

             wangxiaoer.put("birthday","1990-1-1");

             wangxiaoer.put("school","蓝翔");

             wangxiaoer.put("major",new String{}["理发","挖掘机"]);

             wangxiaoer.put("has_girfrienf",false);

             wangxiaoer.put("house",nullobject);

             wangxiaoer.put("comment","这是注释");

             System.out.printh(new JsonObject (wangxiaoer).toString());

}

3、javabean 创建Json对象,通过new JsonObject(对象)

           public class jsonBean{

                 private String name;

                  ........

}

           private static void creatJsonBean(){

                 jsonBean wangxiaoer = new jsonBean();

                 wangxiaoer.set1("name","王小二");

                 wangxiaoer.set2("age",12);

                 wangxiaoer.set3("birthday","1990-1-1");

                 wangxiaoer.set4("school","蓝翔");

                 wangxiaoer.set5("major",new String{}["理发","挖掘机"]);

                 wangxiaoer.set6("has_girfrienf",false);

                 wangxiaoer.set7("house",nullobject);

                 wangxiaoer.set8("comment","这是注释");

                 System.out.printh(new JsonObject (wangxiaoer).toString());

}

}

posted @ 2017-02-28 20:02  方木丶  阅读(1052)  评论(0)    收藏  举报