如何用java实现bin格式转换到json格式
2016-11-07 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
用java实现bin格式转换到json格式,代码如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtils {
public static String encodeList(Object[] objs) {
return encodeList(Arrays.asList(objs));
}
@SuppressWarnings("unchecked")
public static String encodeList(Collection objs) {
JSONArray list = new JSONArray();
if (objs == null || objs.size() == 0)
return list.toString();
for (Object ae : objs) {
list.add(ae);
}
return list.toString();
}
@SuppressWarnings("unchecked")
public static String encodeObject(Object obj) {
if (obj instanceof Collection)
return encodeList((Collection) obj);
JSONObject jo = JSONObject.fromObject(obj);
return jo.toString();
}
@SuppressWarnings("unchecked")
public static <T> List<T> decodeList(String str, Class<T> clazz) {
if (str == null || "".equals(str))
return null;
JSONArray obj = JSONArray.fromObject(str);
Object[] rts = obj.toArray();
List<T> result = new ArrayList<T>(rts.length);
for (int i = 0; i < rts.length; i++) {
Object jo = rts[i];
T ele = (T) JSONObject.toBean((JSONObject) jo, clazz);
result.add(ele);
}
return result;
}
@SuppressWarnings("unchecked")
public static <T> T decodeObject(String json, Class<T> clz) {
JSONObject jsonObject = JSONObject.fromObject(json);
T bean = (T) JSONObject.toBean(jsonObject, clz);
return bean;
}
public static void main(String[] args) {
DTO d = new DTO();
d.setName("dto");
Son realSone = new Son();
realSone.setName("father,son");
realSone.setSonName("son1");
realSone.setAge(1);
d.setRealSon(realSone);
Son realSone2 = new Son();
realSone.setName("father,son2");
realSone.setSonName("son2");
realSone.setAge(2);
d.setSon(realSone2);
List<Father> sonList = new ArrayList<Father>();
sonList.add(realSone);
sonList.add(realSone2);
d.setSonList(sonList);
String json=encodeObject(d);
System.out.println(encodeObject(d));
//JSONObject parseObject = JSON.parseObject(json);
JSONObject fromObject = JSONObject.fromObject(json);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("realSon", Son.class);
classMap.put("son", Son.class);
classMap.put("sonList", Father.class);
DTO newDto = (DTO) fromObject.toBean(fromObject, DTO.class, classMap);
System.out.println(newDto.getSonList().get(0).getName());
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtils {
public static String encodeList(Object[] objs) {
return encodeList(Arrays.asList(objs));
}
@SuppressWarnings("unchecked")
public static String encodeList(Collection objs) {
JSONArray list = new JSONArray();
if (objs == null || objs.size() == 0)
return list.toString();
for (Object ae : objs) {
list.add(ae);
}
return list.toString();
}
@SuppressWarnings("unchecked")
public static String encodeObject(Object obj) {
if (obj instanceof Collection)
return encodeList((Collection) obj);
JSONObject jo = JSONObject.fromObject(obj);
return jo.toString();
}
@SuppressWarnings("unchecked")
public static <T> List<T> decodeList(String str, Class<T> clazz) {
if (str == null || "".equals(str))
return null;
JSONArray obj = JSONArray.fromObject(str);
Object[] rts = obj.toArray();
List<T> result = new ArrayList<T>(rts.length);
for (int i = 0; i < rts.length; i++) {
Object jo = rts[i];
T ele = (T) JSONObject.toBean((JSONObject) jo, clazz);
result.add(ele);
}
return result;
}
@SuppressWarnings("unchecked")
public static <T> T decodeObject(String json, Class<T> clz) {
JSONObject jsonObject = JSONObject.fromObject(json);
T bean = (T) JSONObject.toBean(jsonObject, clz);
return bean;
}
public static void main(String[] args) {
DTO d = new DTO();
d.setName("dto");
Son realSone = new Son();
realSone.setName("father,son");
realSone.setSonName("son1");
realSone.setAge(1);
d.setRealSon(realSone);
Son realSone2 = new Son();
realSone.setName("father,son2");
realSone.setSonName("son2");
realSone.setAge(2);
d.setSon(realSone2);
List<Father> sonList = new ArrayList<Father>();
sonList.add(realSone);
sonList.add(realSone2);
d.setSonList(sonList);
String json=encodeObject(d);
System.out.println(encodeObject(d));
//JSONObject parseObject = JSON.parseObject(json);
JSONObject fromObject = JSONObject.fromObject(json);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("realSon", Son.class);
classMap.put("son", Son.class);
classMap.put("sonList", Father.class);
DTO newDto = (DTO) fromObject.toBean(fromObject, DTO.class, classMap);
System.out.println(newDto.getSonList().get(0).getName());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询