fastJson常用方法
{} 双括号表示对象 [] 中括号表示数组 "" 双引号内是属性或值 : 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)
public static final Object parse(String text); // 把JSON文本转换成JSONObject或者JSONArray public static final JSONObject parseObject(String text); // 把JSON文本转换成JSONObject public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本转换成JavaBean public static final JSONArray parseArray(String text); // 把JSON文本转换成JSONArray public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本转换成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 public static final Object toJson(Obiect javaObject); //将JavaBean转换为JSONObject或者JSONArray
JSONObject、JSONArray 示例:
String s2 = "[ "+ " { "+ " \"id\": 1 "+ " \"type\": \"cycle\", "+ " \"attribute\": { "+ " \"center\": \"(10.4, 123.345)\", "+ " \"radius\": 67.4 "+ " } "+ " }, "+ " { "+ " \"id\": 2, "+ " \"type\": \"polygon\", "+ " \"attribute\": [ "+ " { "+ " \"vertex\": \"(10.4,133.345)\" "+ " }, "+ " { "+ " \"vertex\": \"(10.4,143.345)\" "+ " } "+ " ] "+ " } "+ "] "; JSONArray ja = JSON.parseArray(s2); JSONObject jo = ja.getJSONObject(1) ; JSONArray ja1 = jo.getJSONArray("attribute"); JSONObject jo1 = ja1.getJSONObject(1) ; String s = jo1.getString("vertex"); System.out.println(s);//结果为(10.4,143.345)
java的JsonObject对象提取值
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.swagger.models.auth.In; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.Iterator; public class test { public static void main(String[] args) throws Exception { //第一种格式 /** * [ { "data ": [ { "building_id ": "*** ", "building_num ": "** ", "door_name ": "** ", "electric ": "** ", "room_name ": "** " } ], "success ": true } ] */ String s="[{\"success\":true,\"data\":[{\"building_id\":\"***\",\"building_num\":\"**\",\"room_name\":\"**\",\"door_name\":\"**\",\"electric\":\"**\"}]}]" ; String b= s.substring(0,s.length()-1); String c=b.substring(1, b.length()); System.out.println(b+"b___"); JSONObject jsonx = JSON.parseObject(c); JSONArray ja = jsonx.getJSONArray("data"); for (int i = 0; i < ja.size(); i++) { JSONObject jo = ja.getJSONObject(i); String building_id = jo.getString("building_id"); System.out.println(building_id+"building_id>>>>>"); } //第二种格式 /** * [ { "password ": "*1234567890 ", "success ": "true " } ] */ String s="[{\"success\":\"true\",\"password\":\"*1234567890\"}]"; String b= s.substring(0,s.length()-1); String c=b.substring(1, b.length()); System.out.println(c+"c___"); JSONObject reagobj = JSONObject.fromObject(c); String name = reagobj.getString("password"); System.out.println(name+"name,,,,,,"); String password = jm.getString("password"); System.out.println(password); System.out.println("看看有没有值"+password); //第三种格式 /** * { "data ": { "access_token ": "5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206 ", "expires_second ": 36000 }, "rlt_code ": "HH0000 ", "rlt_msg ": "成功 " } */ String res="{\"data\":{\"access_token\":\"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206\",\"expires_second\":36000},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}"; JSONObject jsonObject= JSON.parseObject(res); String data = jsonObject.getString("data"); JSONObject jsondata= JSON.parseObject(data); String token = jsondata.getString("access_token"); //第四种格式 /** * { "data ": { "total ":23, "start ":0, "total_page ":3, "rows ": [ { "op_way ": "3 ", "user_mobile ": "15321918571 ", "op_time ":1493881391976, "pwd_no ":30}, { "op_way ": "1 ", "op_time ":1493880995000, "pwd_no ":31} ], "current_page ":1, "page_size ":10 }, "rlt_code ": "HH0000 ", "rlt_msg ": "成功 " } */ String res="{\"data\":{\"total\":23,\"start\":0,\"total_page\":3,\"rows\":[{\"op_way\":\"1\",\"op_time\":1493884964000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493884615032,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"18518517491\",\"op_time\":1493883836552,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493883294000,\"pwd_no\":31},{\"op_way\":\"1\",\"op_time\":1493883256000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493883015371,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493882007000,\"pwd_no\":31},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881498520,\"pwd_no\":30},{\"op_way\":\"3\",\"user_mobile\":\"15321918571\",\"op_time\":1493881391976,\"pwd_no\":30},{\"op_way\":\"1\",\"op_time\":1493880995000,\"pwd_no\":31}],\"current_page\":1,\"page_size\":10},\"rlt_code\":\"HH0000\",\"rlt_msg\":\"成功\"}"; JSONObject jsonObject= JSON.parseObject(res); String data = jsonObject.getString("data"); JSONObject jsonObjects= JSON.parseObject(data); JSONArray ja = jsonObjects.getJSONArray("rows"); for (int i = 0; i < ja.size(); i++) { JSONObject jo = ja.getJSONObject(i); String op_way = jo.getString("op_way"); String op_time = jo.getString("op_time"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long lt = new Long(op_time); Date date = new Date(lt); res = simpleDateFormat.format(date); String pwd_no = jo.getString("pwd_no"); String user_mobile = jo.getString("user_mobile"); System.out.println(op_way+res+pwd_no+user_mobile+"------------"); } } }
https://blog.csdn.net/qciwyy/article/details/80669247
JSON.toJSONStringWithDateFormat 示例:
Date date=new Date(); System.out.println(JSON.toJSONString(date));//输出毫秒值 1557307666357 //默认格式为yyyy-MM-dd HH:mm:ss System.out.println(JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));//"2019-05-08 17:27:46" //根据自定义格式输出日期 String s1 = JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat); System.out.println(s1);//"2019-05-08"
部分来自:https://www.cnblogs.com/yb38156/p/9821811.html