java中将数组、对象、Map、List转换成JSON数据
如果要将数组、对象、Map、List转换成JSON数据,那我们需要一些jar包:
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
commons-logging.jar
commons-lang.jar
commons-collections.jar
commons-beanutils.jar
maven依赖
https://blog.csdn.net/baidu_35468322/article/details/81207337
// 将数组转换为JSON:
String[] arr = {"asd","dfgd","asd","234"};
JSONArray jsonarray = JSONArray.fromObject(arr);
System.out.println(jsonarray);
// 对象转换成JSON:
UserInfo user = new UserInfo(1001,"张三");
JSONArray jsonArray = JSONArray.fromObject(user);
System.out.println( jsonArray );
// 把Map转换成json, 要使用jsonObject对象:
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId", 1001);
map.put("userName", "张三");
map.put("userSex", "男");
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println(jsonObject);
// 把List转换成JSON数据:
List<UserInfo> list = new ArrayList<UserInfo>();
UserInfo user = new UserInfo(1001, "张三");
list.add(user);
list.add(user);
list.add(user);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
java中将数组、对象、Map、List转换成JSON数据的更多相关文章
- 将字典或者数组转换成JSON数据或者字符串
将字典或者数组转换成JSON数据或者字符串 源码: NSDictionary+JSON.h 与 NSDictionary+JSON.m // // NSDictionary+JSON.h // Cat ...
- js如何把字符串转换成json数据的方法
js如何把字符串转换成json数据的方法 function strtojson(str){ var json = eval('(' + str + ')'); return json; } 方法二 f ...
- C# DataTable 转换成JSON数据
原文:C# DataTable 转换成JSON数据 using System; using System.Collections.Generic; using System.Data; using S ...
- 如何将java对象转换成json数据
package cn.hopetesting.com.test;import cn.hopetesting.com.domain.User;import com.fasterxml.jackson.c ...
- Java中将16进制字符串转换成汉字
技术交流群:233513714 /** * 将16进制字符串转换成汉字 * @param str * @return */ public static String deUnicode(String ...
- java中object数据怎么转换成json数据
可以通过这个(json-lib-2.3-jdk15.jar)jar里的方法转换 JSONObject json = JSONObject.fromObject(Object); 如果对象数组 JSON ...
- Java中将一个反斜杠转换成两个反斜杠
代码示例: s = s.replaceAll("\\\\", "\\\\\\\\");
- SpringMVC中使用@ResponseBody注解将任意POJO对象返回值转换成json进行返回
@ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区. ...
- Map集合转成json数据
maven项目需要导入一下依赖: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId> ...
随机推荐
- HashMap 1.8的源码分析三
线程安全问题: 在添加时候并没有进行安全考虑,枷锁 所以是线程不安全的,接下来进行代码测试; package com.mmall.concurrency.example.commonUnsafe; i ...
- java Pattern和Matcher完全解析
基本使用: 本文不讲解正则表达式,需要请看API Scanner中的使用正则表达式 //Scanner 支持的分组 Scanner cin=new Scanner("red a bbc&qu ...
- CodeForces - 124B-Permutations(DFS)
You are given n k-digit integers. You have to rearrange the digits in the integers so that the diffe ...
- P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler
传送门 题目问的是从出发点一直跑到终点的一条链上所有齿轮的速度和 其他的不用考虑 直接搜就好了 注意求的是绝对值之和,不是和的绝对值,所以不用考虑方向问题 注意 N<=1050 数组不要只开10 ...
- P2925 [USACO08DEC]干草出售Hay For Sale
传送门 把每体积的干草价值看成一,就变成求最大价值 直接上背包就行了 注意优化常数 #include<iostream> #include<cstdio> #include&l ...
- SQL注入工具sqlmap的注入过程记录
1.sqlmap的get注入 假设目标是 https://www.baidu.com/news.php?id=1&data=2 sqlmap语句 列库 sqlmap.py -u "h ...
- sqlserver 常用语法
sqlserver查找 table, view, column select * from information_schema.tables where table_schema='bk' sele ...
- php数组·的方法-数组检索
/* * //数组检索函数 * */ //array_keys() 获取数组中所有键名 //array_values() 获取数组中所有键名 $arr6=range('a','e'); print_r ...
- Java 实践
/** *宠物就是一个标准,包含多类宠物 *定义宠物标准接口Pet *定义Cat和Dog两个Pet接口的子类 *使用链表结构动态存储宠物信息 *定义商店类(工厂类),负责宠物的上架(链表添加).下架( ...
- Mongodb installation & userguide
1.Mongodb Installation in Ubuntu (1) Download from: https://www.mongodb.org/downloads File: mongodb- ...