Json在前后台传输中,是使用最多的一种数据类型。json生成的方法有很多,自己只是很皮毛的知道点,用的时候,难免会蒙。现在整理下

第一种:

import net.sf.json.JSONArray;
   import net.sf.json.JSONObject;

1、bean转为Json

User u =  User();
u.setAge(22);
u.setUsername("hzucmj");
u.setEnabled(); JSONObject json = JSONObject.fromObject(u);
System.out.println(json.toString());

2、List转为Json

User u1 =  User();
u1.setAge(22);
u1.setUsername("hzucmj");
u1.setEnabled(); User u2 = User();
u2.setAge(20);
u2.setUsername("ctf");
u2.setEnabled(); List<Object> list = ArrayList<Object>();
list.add(u1);
list.add(u2); JSONArray json = JSONArray.fromObject(list);
System.out.println(json.toString());

3、Map转为Json

HashMap<String, Comparable> map =  HashMap<String, Comparable>();
map.put("name", "hzucmj");
map.put("age", 22);
JSONObject json = JSONObject.fromObject(map);
System.out.println(json.toString());

4.创建json对象

JSONObject jsonObj = new JSONObject();
jsonObj.put("id",1);
jsonObj.put("name","张勇");
jsonObj.put("sex","男");
jsonObj.put("age",24);
jsonObj.put("hobby",new String[]{"上网","游戏","跑步","音乐"});//这里就是一个String数组:String hobby[];(hobby:爱好)
System.out.println("我创建的json:"+jsonObj.toString());

5.json对象转java对象

Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
System.out.println(stud.getName());

6.创建json对象并添加属性

       JSONObject json1 = new JSONObject();
json1.put("Int_att",25);//添加int型属性
json1.put("String_att","str");//添加string型属性
json1.put("Double_att",12.25);//添加double型属性
json1.put("Boolean_att",true);//添加boolean型属性 JSONObject json = new JSONObject();
json.put("年龄",25);//添加int型属性
json.put("名字","段鑫杰");//添加string型属性 //添加JSONObject型属性
JSONObject json2 = new JSONObject();
json2.put("id", 1);
json2.put("name", "tom");
json1.put("JSONObject_att",json);
//添加JSONArray型属性
JSONArray jsonArray = new JSONArray();
jsonArray.add("array0");
jsonArray.add("array1");
jsonArray.add("array2");
jsonArray.add("array3");
json1.put("JSONArray_att", jsonArray); System.out.println(json1.toString());
System.out.println("获取名字:"+json1.getJSONObject("JSONObject_att").get("名字"));
System.out.println("JSONArray_att:"+json1.getJSONArray("JSONArray_att"));

第二种:

import com.alibaba.fastjson.JSONArray;
   import com.alibaba.fastjson.JSONObject;

        String jsonArray = JSONArray.toJSONString(list);
System.out.println("List转换json:"+jsonArray);

直接调用 JSONArray.toJSONString(list)就行,感觉参数基本上都适合。帅吧,毕竟是阿里巴巴嘛。

总结:两者都是基于JDK的轻量级的。菜鸟一枚,多谢大家指点!

 

java用JSONObject生成json的更多相关文章

  1. 在JAVA中使用JSONObject生成json

    JSON是一种轻量级的数据交换格式,在现在的web开发中,是非常常见的.在没有方便的工具之前,我们或许会使用拼字符串的形式来生成json数组,今天我们使用一个json-lib.jar包来为我们实现生成 ...

  2. Java Servlet生成JSON格式数据并用jQuery显示

    1.Servlet通过json-lib生成JSON格式的数据 import java.io.IOException;import java.io.PrintWriter;import java.uti ...

  3. [转]java中JSONObject与JSONArray的使用详细说明及有关JSON的工具类

    JSONObject与JSONArray的使用 一.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.c ...

  4. java生成Json工具之JsonSimple的使用

    json-simple是由是Google开发的Java JSON解析框架,基于Apache协议.目前版本为1.1 项目主页:https://code.google.com/p/json-simple/ ...

  5. java对象转json应clone,避免生成json串有问题

    今天因为一个java对象转json,搞了我一下午,在些记录一下: 是这样:我在strtuts2的action中调用services返回 Row: 26, 中国银行海鹰, 29, 东楼, 36, 1F ...

  6. 使用JSONObject生成和解析json

    1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组,以"[]"括起来 Object 对象,类似于C中的结构体 ...

  7. Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

    18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...

  8. java 字符串解析为json 使用org.json包的JSONObject+JSONArray

    参考: https://blog.csdn.net/xingfei_work/article/details/76572550 java中四种json解析方式 JSONObject+JSONArray ...

  9. 使用JSONObject类来生成json格式的数据

    JSONObject类不支持javabean转json 生成json格式数据的方式有: 1.使用JSONObject原生的来生成 2.使用map构建json格式的数据 3.使用javabean来构建j ...

随机推荐

  1. [CF1234F] Yet Another Substring Reverse - 字符串,状压DP

    CF1234F Yet Another Substring Reverse Description 给定一个字符串,可以任意翻转一个子串,求最终满足所有字符互不相同的子串的最大长度. 数据范围: \( ...

  2. Oracle 11g安装 —— Oracle Database 11g Release2 for Windows(x64)

    文章来自:https://blog.csdn.net/IT_xiao_guang_guang/article/details/104422421 下面是我的Oracle 11g安装过程,希望可以帮到正 ...

  3. 关于jquery ajax不执行success回调函数

    检查error函数是否执行,发现错误信息为parseerror,表示jquery解析返回结果时失败,只需要将ajax参数dataType:"json"改为"text js ...

  4. python之路之反射

    这个是上两个的加强版

  5. mybatis(六):设计模式 - 责任链模式

  6. cc.Sprite 与 ccui.ImageView 改变图片

    sprite.setTexture(fileName); imageView.loadTexture(fileName);

  7. Mybaits(11)延迟加载

    一.概述 1.概念 就是在需要用到数据时才去进行加载,不需要用的数据就不加载数据.延迟加载也称为懒加载. 2.优缺点 优点:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要 ...

  8. MySQL数据库 | 数据表-查询命令详细记录

    本篇专门记录数据库增删改查中最常用.花招最多的 查. [文章结构] 一.数据的准备 二.基本的查询功能 三.条件查询 四.查询排序 五.聚合函数 六.分组查询 七.分页查询 八.连接查询 九.子查询 ...

  9. unittest学习3-测试组件setup、teardown

    unittest的测试用例执行时都可以设置setup.teardown,用来初始化测试开始和测试结束关闭,例如: import unittest class MyTestCase(unittest.T ...

  10. python中pip问题

    1.在cmd中运行pip命令显示‘pip命令显示不是内部或外部命令,也不是可运行的程序或批处理文件’的问题 先看python的安装目录下Script文件夹中pip3.exe有没有缺失 如果没有在cmd ...