数组、List、Set、Map的json怎么写 您所在的位置:网站首页 json描述表格数据例子怎么写 数组、List、Set、Map的json怎么写

数组、List、Set、Map的json怎么写

2024-05-30 07:31| 来源: 网络整理| 查看: 265

在使用Postman进行接口调试post请求的时候,经常会出现需要自己填写json的情况,下面来简单记录下常用类型的json格式。

1.Postman进行post请求的配置 打开Postman,操作如图: 在这里插入图片描述

在这里插入图片描述 2.常用json格式。

int型数组: { "arr" : [80,81,82,83] } string数组 { "arr" : ["a","b","c","d"] } List: { "list" : [14120913 , 14120914 , 14120915 , 14120916] } List: { "list" : ["ASD14120913","ASD14120914","ASD14120915","ASD14120916"] } Map { "map" : {1:"a",2:"b",3:"c",4:"d"} } Set { "set" : ["a","b","c","d"] } List: one={"a","b","c","d"}, Set: two={"A","B","C","D"} Map { "map":{"one":["a","b","c","d"], "two":["A","B","C","D"]} }

3.借助fastjson生成json 现在最主要的是如何填写json,下面给出数组、List、Set、Map基本序列化与日期格式化的示例代码,按需求修改代码; 先下载fastjson,链接如下: 链接:https://pan.baidu.com/s/1e-HwNFVV4Ucg9Vi4CL8J_w 提取码:c20x

import com.alibaba.fastjson.JSON; import java.util.*; public class toJson { public static void main(String[] args) { array2Json(); list2Json(); map2Json(); set2Json(); container2Json(); } /** * Array to JSON */ public static void array2Json(){ // 数组序列化 Integer[] array = new Integer[]{1,2,3,4}; String json = JSON.toJSONString(array); System.out.println("序列化 Array to JSON:" + json); /******************************/ String[] array2 = new String[]{"a","b","c","d"}; String json2 = JSON.toJSONString(array2); System.out.println("序列化 Array to JSON:" + json2); // 数组反序列化 //array = (String[])JSON.parseObject(json,String[].class); } /** * List to JSON */ public static void list2Json(){ // List序列化 List list = new ArrayList(); list.add("ASD14120913"); list.add("ASD14120914"); list.add("ASD14120915"); list.add("ASD14120916"); String json = JSON.toJSONString(list); System.out.println("序列化 List to JSON: " + json); // List反序列化 list = (List)JSON.parseObject(json,List.class); System.out.println("反序列化 List内容:" + list); } /** * Set to JSON */ public static void set2Json(){ // List序列化 Set set = new HashSet(); set.add("a"); set.add("b"); set.add("c"); set.add("d"); String json = JSON.toJSONString(set); System.out.println("序列化 Set to JSON: " + json); // Set反序列化 set = (Set)JSON.parseObject(json,Set.class); System.out.println("反序列化 Set内容: "); System.out.println(set); } /** * Map to JSON */ public static void map2Json(){ Map map = new HashMap(); map.put(1,"a"); map.put(2,"b"); map.put(3,"c"); map.put(4,"d"); String json = JSON.toJSONString(map); System.out.println("序列化Map: " + json); // Map反序列化 map = (Map)JSON.parseObject(json, Map.class); System.out.println("反序列化 Map内容: "); System.out.println(map); } /** * Container to JSON */ public static void container2Json(){ List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); Set set = new HashSet(); set.add("A"); set.add("B"); set.add("C"); set.add("D"); Map map = new HashMap(); map.put("one", list); map.put("two", set); String jsonString = JSON.toJSONString(map); System.out.println("Container to JSON:"); System.out.println(jsonString); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有