JAVA数据转换

1、JSON数组转List对象

List<User> list  = JSONObject.parseArray(jsonArray, User.class);

     List转JSONArray

List<User> users = new ArrayList<User>(); 
JSONArray json = JSONArray.fromObject(users); 

    List转JSONObject

JSONObject jsonObj = new JSONObject(); 
jsonObj.put("users", users); 

2、JSON字符串转对象

User user = JSON.parseObject(jsonString, User.class);

3、Map转JSON字符串

Map<String, Object> paramMap = new HashMap<>();
String jsonString = JSON.toJSONString(paramMap);

//方法2 map中含有对象
Map<String,Car> map = new HashMap<String, Car>(); 
map.put("a",new Car());  
String json = JSON.toJSONString(map,true);

4、Map转JSONObject

// 方法1
Map<String, Object> paramMap = new HashMap<>();
JSONObject json = new JSONObject(paramMap);
// 方法2
JSONObject json = JSONObject.fromObject(map); 

 5、 Map转JSONArray

JSONArray json = JSONArray.fromObject(map); 

6、JSON转Map

JSONObject json = new JSONObject();
Map<String, Object> map = (Map<String, Object>)json;

// 方法2
Map map1 = JSON.parseObject(json);

// 方法3 map中含有对象
Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json);

7、JSON转String

JSONObject json = new JSONObject();
json.put("c", "v");
json.toJSONString();

8、String转JSON

String str = "{\"username\":\"aa\",\"sex\":\"1\"}";
JSONObject json = JSONObject.parseObject(str);

9、对象转JSON数组

User user = new User(); 
JSONArray json = JSONArray.fromObject(user); 

10、对象转JSONObject

JSONObject jsonObj = JSONObject.fromObject(user); 

11、List转数组

List<String> list=new ArrayList<>();
String[] array=list.toArray(new String[0]);

// 在低版本的 Java 中推荐使用初始化大小的数组,因为使用反射调用去创建一个合适大小的数组相对较慢。但是在 openJDK 6 之后的高版本中方法被优化了,传入空数组相比传入初始化大小的数组,效果是相同的甚至有时候是更优的。因为使用 concurrent 或 synchronized 集合时,如果集合进行了收缩,toArray()和size()方法可能会发生数据竞争,此时传入初始化大小的数组是危险的。
// 数组里面的元素只是元素的引用,不是存储的具体元素,所以数组中元素的类型还是保存在Java虚拟机中的。

12、数组转List

String[] strs={"a","b","c"};
// 返回的是定义在java.util.Arrays中一个私有静态类,没有实现其他方法,对list的操作仍然反映在原数组上,因此这个list是定长的,不支持add、remove操作,程序会抛出异常UnsupportedOperationException。
不支持基本类型
List<String> list= Arrays.asList(strs);
// 方法2 优化
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arrays));

// 方法3
List<String> list2 = new ArrayList<String>(arrays.length);
Collections.addAll(list2, arrays);

//八种基本数据类型的话需要用到boxed转换一下
int []te = new int[4];
List<Integer> collect = Arrays.stream(te).boxed().collect(Collectors.toList());
System.out.println(collect);
//非八种基本数据类型直接转换
String []te1 = new String[4];
List<String> collect1 = Arrays.stream(te1).collect(Collectors.toList());
System.out.println(collect1);

List<String> list2 = Arrays.asList("AAA","BBB"); 

13、String转数组

String str = "1,2";

String[] array = str.split(",");

14、数组转String

// commons-lang3.jar
String[] array = {"1", "2"};
String s = StringUtils.join(array, ",");
// 方法2
String s1 = Arrays.toString(array);
String s = s1.substring(1, s1.length()-1);

15、List转String

List list = new ArrayList();
String s = StringUtils.join(list , ",");

// 方法2
List list = new ArrayList();
String s1 = list.toString();
String s = s1.substring(1, s1.length()-1);

16、String转List

String str = "1,2,3";
List list = Arrays.asList(str.split(","));

17、Map Key 转化为List

List<String> mapKeyList = new ArrayList<String>(map.keySet());    

18、将Map value转化为List

List<String> mapValuesList = new ArrayList<String>(map.values()); 

19、将Map 的key转化为Set

Set<String> mapKeySet = map.keySet();  

20、将Map 的value转化为Set

Set<String> mapValuesSet = new HashSet<String>(map.values());

21、数组转Set

Set<String> set = new HashSet<String>(Arrays.asList(arr));

22、Set转数组

Set<String> set = new HashSet<String>();    
String[] arr = new String[set.size()];    
//Set-->数组    
set.toArray(arr);  

23、List转Set

Set<String> listSet = new HashSet<String>(list);

24、Set转List

List<String> setList = new ArrayList<String>(set); 

25、Object转int

int a= Integer.parseInt(obj.toString());

26、Object转List

List<String> list = castList(objectName, String.class);

/**
 * Object 对象转 List
 */
public static <T> List<T> castList(Object obj, Class<T> clazz) {
    List<T> result = new ArrayList<T>();
    if (obj instanceof List<?>) {
        for (Object o : (List<?>) obj) {
            result.add(clazz.cast(o));
        }
        return result;
    }
    return null;
}

27、对象转Map

Map<String, Object> map = new org.apache.commons.beanutils.BeanMap(findArchiveDto);
// 2
Map map = JSONObject.parseObject(JSONObject.toJSONString(findArchiveDto), Map.class);
// 3
Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(findArchiveDto));

28、Map转对象

FindArchiveDto findArchiveDto1 = JSON.parseObject(JSON.toJSONString(maps), FindArchiveDto.class);
// 或
FindArchiveDto dto = new FindArchiveDto();
BeanUtils.populate(dto, map);

29、String转List<Map>

List<Map> maps = JSONArray.parseArray(content, Map.class);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值