json与list,map,String之间的互转
package tools; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import bean.Student; public class Json {
public static void main(String[] args) {
//json对象转json字符串
jsonObjToJsonStr();
//json字符串转json对象
jsonStrToJsonObj();
//list转json
listToJson();
//json转list
jsonToList();
//json转map
jsonToMap();
//map转json
mapToJson();
} /**
* map转json
*/
private static void mapToJson() {
ObjectMapper mapper = new ObjectMapper();
String json = "";
Map<String, String> map = new HashMap<String,String>();
map.put("name", "JSON");
map.put("age", "23");
map.put("address", "北京市西城区");
try {
json = mapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(json);
} /**
* json转map
*/
private static void jsonToMap() {
String json = "{\"name\":\"JSON\",\"address\":\"北京市西城区\",\"age\":23}";
Map<String, String> map = new HashMap<String, String>();
ObjectMapper mapper = new ObjectMapper();
try{
map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){});
System.out.println(map);
}catch(Exception e){
e.printStackTrace();
}
} /**
* json转list
*/
private static void jsonToList() {
//json转成list
String json ="[\"abc\",\"123\"]";
JSONArray jsonArray = JSONArray.fromObject(json);
List<String> list = (List) JSONArray.toCollection(jsonArray);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} /**
* list转json
*/
private static void listToJson() {
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("123");
//list转成json
String json =JSONArray.fromObject(list).toString();
System.out.println(json);
//运行:["abc","123"]
} /**
* json字符串转json对象
*/
private static void jsonStrToJsonObj() {
//json转为json对象,取属性值
String json ="{\"name\":\"JSON\",\"address\":\"北京市西城区\",\"age\":23}";
JSONObject jo = (JSONObject) JSONObject.parse(json);
String add = jo.getString("address");
System.out.println(add);
} /**
* json对象转json字符串
*/
private static void jsonObjToJsonStr() {
Student stu=new Student();
stu.setName("JSON");
stu.setAge(23);
stu.setAddress("北京市西城区");
//对象转换为json字符串
Gson gson = new Gson();
String json = gson.toJson(stu);
System.out.println(json);
}
}
json与list,map,String之间的互转的更多相关文章
- JSONObject,JSONArray,Map,String之间转换
http://blog.csdn.net/superit401/article/details/51727739 1.String转JSONObject String jsonMessage = &q ...
- java和js中JSONObject,JSONArray,Map,String之间转换
--------------------------------------------------Java中--------------------------------------------- ...
- JSON对象和String之间的互转及处理
如题,本文列举了一些在web前端开发中常用的转换及处理方式.使用JSON而不是字符串,主要是为了方便处理. JSON:JavaScript 对象表示法(JavaScript Object Notati ...
- int 和String之间的互转
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- List<Map<String, Object>> 与 json 互转
近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件. 思路是 1.定时查询指纹,存到txt缓存文件中. 2.新增或删除指纹时,查询指纹,存到txt缓存文 ...
- C++实现json字符串与map的转换
开源资源库 jsoncpp-src-0.5.0.tar.gz:https://sourceforge.net/projects/jsoncpp/ jsoncpp-master.ziphttps://g ...
- fastjson对象,JSON,字符串,map之间的互转
1.对象与字符串之间的互转 将对象转换成为字符串 String str = JSON.toJSONString(infoDo); 字符串转换成为对象 InfoDo infoDo = JSON.pars ...
- 总结的一些json格式和对象/String/Map/List等的互转工具类
总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...
- Json、JavaBean、String等互转
Json.JavaBean.String等互转 本文介绍简单的Json.JavaBean.String互换(下文JavaBean简称Object对象,这里不是很严谨) 转换关系如下: 其中String ...
随机推荐
- (转)Python 3 collections.defaultdict() 与 dict的使用和区别
原文:https://www.cnblogs.com/herbert/archive/2013/01/09/2852843.html 在Python里面有一个模块collections,解释是数据类型 ...
- yum安装报错:Failure when receiving data from the peer
系统:Centos6.9 操作:yum install -y *.rpm 报错信息: Transaction Summary ===================================== ...
- Vue.js系列之一初识Vue
在看vue.js之前,可以先看这两篇文章,对于为什么要使用vue会有一定帮助 1.Vue.js !important 2.界面之下:还原真实的MV*模式 !important 3.web前端优化之re ...
- ActiveMQ学习--001--ActiveMQ和消息中间件
一.ActiveMQ简介 1,ActiveMQ是什么 ActiveMQ是Apache推出的开源的,完全支持JMS1.1和J2EE 1.4规范的JMS Provider实现的消息中间件(MOM) 2, ...
- Android 开发工具类 31_WebService 获取手机号码归属地
AndroidInteractWithWebService.xml <?xml version="1.0" encoding="utf-8"?> & ...
- Scope及其子类介绍
之前写的文章: 关于作用域范围Scope Scope及相关的子类如下: 同时有些Scope还继承了Scope.ScopeListener类,如下: 1.StarImportScope及ImportSc ...
- C语言利用异或进行两个值的交换
异或有两个很重要的性质: 1. A^A = 0; 2.A^0 = A; 利用这两个性质,我们就能够利用异或进行两个值的交换. 代码如下: #include <stdio.h> int ma ...
- WPF多路绑定
WPF多路绑定 多路绑定实现对数据的计算,XAML: 引用资源所在位置 xmlns:cmlib="clr-namespace:CommonLib;assembly=CommonLib&q ...
- Hdfs数据备份
Hdfs数据备份 一.概述 本文的hdfs数据备份是在两个集群之间进行的,如果使用snapshot在同一个集群上做备份,如果datanode损坏或误操作清空了数据,这样的备份就无法完全保证数据安全性. ...
- CC2530自动安全联网
CC2530自动联网的苦恼 不知道博客园里面有没有人研究CC2530,也就是zigbee技术,其实我在做东西的时候很纠结一个问题,那就是我如何将设备连上后,如何通过协调者发送命令给终端呢?有什么方法可 ...