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> ...
随机推荐
- python+splinter实现12306网站刷票并自动购票流程
python+splinter实现12306网站刷票并自动购票流程 通过python+splinter,实现在12306网站刷票并自动购票流程(无法自动识别验证码). 此类程序只是提高了12306网站 ...
- java中Excel导出
转载:https://www.cnblogs.com/gudongcheng/p/8268909.html,稍加修改了 https://www.cnblogs.com/hanfeihanfei/p/7 ...
- KS光盘制作 for rhel6.5 and rhel7.2
############################## RHEL6.5 KS光盘制作--1.复制光盘到本地mkdir -p /opt/rhel6mount /dev/cdrom /mediacp ...
- 单点登录-JWT(Json Web Tokens)
来自:http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html 1.跨域认证 1.用户向服务器发送用户名和密码. 2.服务 ...
- spring boot 启动时运行代码(2)ApplicationListener
项目概览: StepExecutor: @Component @Slf4j public class StepExecutor implements Runnable { @Autowired pri ...
- my02_Atlas mysql5.7安装配置
软件环境:centos7.3,glib-2.49,lua5.1,Atlas2.2.1,mysql5.7 依赖包安装******************************************* ...
- java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue
LikedList: package Date_pacage; public class LinkedList<E> { public static void main(String[] ...
- swagger demo code
//Application 开启注解 @EnableSwagger2public class Application { public static void main(String[] args) ...
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String
http://codeforces.com/contest/914/problem/F 以前做过一个类似的,但是查询的子串长度最多是10,这个时候就是用bit + hash做了.这是因为改变一个字符, ...
- leetcode 196. Delete Duplicate Emails 配合查询的delete
https://leetcode.com/problems/delete-duplicate-emails/description/ 题意要对原来的数据表进行删除,不删除不行,它每次只输出原来那个表. ...