其他类型转化为json

时间:2022-04-11 06:39:48
  1. List集合转换成json代码

List list = newArrayList();

list.add( “first” );

list.add( “second” );

JSONArray jsonArray2 =JSONArray.fromObject( list );

  1. Map集合转换成json代码

Map map = newHashMap();

map.put(“name”,”json”);

map.put(“bool”,Boolean.TRUE);

map.put(“int”, newInteger(1));

map.put(“arr”, new String[] {“a”, “b” });

map.put(“func”, “function(i){return this.arr[i]; }”);

JSONObject json =JSONObject.fromObject(map);

  1. Bean转换成json代码

JSONObject jsonObject =JSONObject.fromObject(new JsonBean());

  1. 数组转换成json代码

boolean[] boolArray = newboolean[] { true, false, true };

JSONArray jsonArray1 =JSONArray.fromObject(boolArray);

  1. 一般数据转换成json代码

JSONArray jsonArray3 =JSONArray.fromObject(“[‘json’,’is’,’easy’]” );