何隆昌fastJson格式解析

fastjson 将json字符串转化成List<Map<String, Object>>

JSON.parseObject(jsonstr, new TypeReference<List<Map<String, Object>>>() {});

将json字符串转化成Map<String, Object>

Map<String, Object> map=JSONObject.parseObject(jsonstr,Map.class)
//这里用Map接收也行
阿里巴巴JSONArray转List<JSONObject>
List<BaseBuild> buildList = new ArrayList<BaseBuild>();  对象集合
	buildList = JSONObject.parseArray(jsonArray.toJSONString(), BaseBuild.class);

JSON对象添加元素

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(key, value);
        return jsonObject.toString();

JSON对象转 JSON 字符串 (List集合转字符串也行)

	JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "wjw");
        jsonObject.put("age", 22);
        String jsonStr = JSONObject.toJSONString(jsonObject);
        System.out.println(jsonStr);
        执行结果:
{"name":"wjw","age":22}

List集合转json字符串

String jsonStr = JSONObject.toJSONString(list,new PascalNameFilter(), SerializerFeature.WriteMapNullValue);
//参数一个以上都可以
注意: 默认不输出为空的属性
想要输出空的,就要加属性  SerializerFeature.WriteMapNullValue

new PascalNameFilter()  //首字母大写  [{"Id":86108,"Name":"一分部","ShortName":"一分部"}]


	默认是以英文字母排序的(日期默认转成时间戳)
		在类上加@JSONType(orders={"id","name"})排序
		或者字段上加@JSONField(ordinal = 1)排序

		
	实体类想要不输出某个字段,可以在实体类的字段上加@JSONField(serialize=false)
	想要输出的字段改变名称,也可以在字段上加@JSONField(name="ID")

看下SerializerFeature属性
https://blog.csdn.net/u010246789/article/details/52539576在这里插入图片描述

JSON 字符串 转 JSON对象

  String jsonStr = "{\"school\":\"商职\",\"sex\":\"男\",\"name\":\"wjw\",\"age\":22}";
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        System.out.println(jsonObject.getString("name"));
        System.out.println(jsonObject.getInteger("age"));

JSON字符串数组转JSON对象数组

String json="[{"id:1","name:"小明"}]";
JSONArray jsonArray = JSON.parseArray(json);

JSON对象转Java对象

 String jsonStr = "{\"school\":\"商职\",\"sex\":\"男\",\"name\":\"wjw\",\"age\":22}";
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        System.out.println(jsonObject.getString("name"));
        System.out.println(jsonObject.getInteger("age"));
        
BaseDept dept = JSONObject.toJavaObject(jsonObject , BaseDept.class);

Java对象转JSON对象

BaseDept dept=new BaseDept
 String personStr = JSONObject.toJSONString(dept);  // 转换为json字符串
        System.out.println("personStr:"+personStr);
        JSONObject personObject = JSONObject.parseObject(personStr);  // 转换为json对象

JSON字符串直接转List

 String json= "{\"school\":\"商职\",\"sex\":\"男\",\"name\":\"wjw\",\"age\":22}";
 List<BaseDept> list = JSON.parseArray(json,BaseDept.class);

json-lib.jar/

json数组和List转换
使用的是json-lib.jar包
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

 String json="[{'name':'huangbiao','age':15},{'name':'liumei','age':14}]";  
        JSONArray jsonarray = JSONArray.fromObject(json);  
        System.out.println(jsonarray);  
        List list = (List)JSONArray.toCollection(jsonarray, Person.class); 
         List list = (List)JSONArray.toList(jsonarray, Person.class);
           List list = (List)JSONArray.toList(jsonarray, new Person(), new JsonConfig()); 

版权声明:本文为weixin_43826491原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。