博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java json和object互换
阅读量:6983 次
发布时间:2019-06-27

本文共 2118 字,大约阅读时间需要 7 分钟。

package
 com.help; 



import
 java.io.IOException; 

import
 java.io.InputStream; 

import
 java.io.StringWriter; 

import
 java.util.List; 



import
 org.codehaus.jackson.JsonGenerationException; 

import
 org.codehaus.jackson.JsonParseException; 

import
 org.codehaus.jackson.map.JsonMappingException; 

import
 org.codehaus.jackson.map.ObjectMapper; 



public
 
class
 JsonFunc { 

static
 ObjectMapper mapper = 
new
 ObjectMapper(); 



public
 
static
 String toJSON(Object obj) { 

         StringWriter writer = 
new
 StringWriter(); 

         
try
 { 

                mapper.writeValue(writer, obj); 

         } 
catch
 (JsonGenerationException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 
catch
 (JsonMappingException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 
catch
 (IOException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 

         
return
 writer.toString(); 



public
 
static
 <T> T fromJSON(String json, Class<T> clazz) { 

         ObjectMapper mapper = 
new
 ObjectMapper(); 

         
try
 { 

                
return
 mapper.readValue(json, clazz); 

         } 
catch
 (JsonParseException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 
catch
 (JsonMappingException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 
catch
 (IOException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 

    } 

public
 
static
 <T> T fromJSON(InputStream json, Class<T> clazz) { 

         ObjectMapper mapper = 
new
 ObjectMapper(); 

         
try
 { 

                
return
 mapper.readValue(json, clazz); 

         } 
catch
 (JsonParseException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 
catch
 (JsonMappingException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 
catch
 (IOException e) { 

                
throw
 
new
 RuntimeException(e); 

         } 

    } 

public
 
static
 List<String> getJsonList(String jstr,List<String> li) 


char
[] cstr = jstr.toCharArray(); 

boolean
 bend = 
false

int
 istart =0; 

int
 iend =0; 

for
(
int
 i=0 ; i < cstr.length; i++) 


if
 ((cstr[i] == '{') && !bend) 


istart = i; 


if
 (cstr[i] == '}' && !bend) 


iend = i; 

bend = 
true




if
 (istart !=0) 


String substr = jstr.substring(istart, iend+1); 

jstr = jstr.substring(0,istart-1) + jstr.substring(iend+1,jstr.length()); 

substr = substr.replace(
",\"children\":"
, ""); 

substr = substr.replace(
"]"
, ""); 

substr = substr.replace(
"["
, ""); 

    li.add(substr); 

    getJsonList(jstr,li); 


return
 li; 


}

本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751930,如需转载请自行联系原作者

你可能感兴趣的文章
poj 题型分类
查看>>
二分查找算法及其变种
查看>>
一个泛型冒泡排序的实现
查看>>
大型分布式网站架构设计与实践 第一章《面向服务的体系架构(SOA)》
查看>>
[From OpenBSD Man Page]PFSYNC
查看>>
自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 一)...
查看>>
hdu 5131 Song Jiang&#39;s rank list 【2014ACM/ICPC亚洲区广州站-重现赛】
查看>>
Moose File System分布文件系统测试
查看>>
mysql 高可用方案漫谈(二)
查看>>
React Native微信支付开发爬坑之旅
查看>>
年轻人的第一篇博客
查看>>
使用 Vue + Flask 搭建单页应用
查看>>
10分钟免费开启全站https
查看>>
《js高程》 基本概念 上
查看>>
Sketch 快捷键
查看>>
javascript闭包,你大爷永远是你大爷
查看>>
android社会化分享
查看>>
PHPer面试指南-前言
查看>>
git 常用命令行
查看>>
前端面试——初(H)入(T)江(M)湖(L)
查看>>