JSONObjectString、实体类list转换
JSONObject获取java list
JSONObject -->> JSONArray
jsonObject .getJSONArray("list")
JSONArray --> list
JSONArray.JSONString(), bean.class)
JSONObject与String、实体类互相转换
//实体类对象转换成String类型的JSON字符串
//String类型的JSON字符串转换成实体类对象
/
/Json字符串转换成JSONObject对象
JSONObject.parseObject(JSON字符串) == JSONObject
//JSON字符串转换成实体类对象
JSONObject.parseObject(JSON字符串,实体类对象) == 实体
eg:
User u = new User();
String jsonStr = JSONString(u);
String jsonStr = "{\"userName\":\"admin\"}";
Refund r = JavaObject(jsonStr,User.class);
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
User u = JSONObject.parseObject(jsonStr,User.class);
集合转换
List<ExamListInVO> list = new ArrayList<ExamListInVO>();
list = JSONObject.parseArray(strResult, ExamListInVO.class);
JSONObject获取JSONArray转JSONObject获取某个属性值String httpPost4Instream = http.httpPost4Instream
JSONObject jsonbject = JSONObject.fromObject(httpPost4Instream);
JSONArray jsonArray = JSONArray("stores");
String string = String(0);
JSONObject fromObject = JSONObject.fromObject(string);
String startDate = String("startDate");
String endDate = String("endDate");
param.setFromDate(Long.valueOf(startDate).longValue());
param.setToDate(Long.valueOf(endDate).longValue());
object to————————————————
版权声明:本⽂为CSDN博主「weixin_40650583」的原创⽂章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原⽂出处链接及本声明。原⽂链接:blog.csdn/weixin_40650583/article/details/84821361
--------------------------------------------------java中--------------------------------------------------------------
1.String转JSONObject
(1)json-lib(即net.sf.json.JSONObject)
String jsonMessage = "{\"语⽂\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}";
JSONObject myJson = JSONObject.fromObject(jsonMessage);
(2).⽤阿⾥巴巴的fastjson的jar包
String str = "{\"baid\":null,\"32d3:\":\"null\",433:\"0x32\",032:\"ju9fw\"}";
com.alibaba.fastjson.JSONObject jm = com.alibaba.fastjson.JSON.parseObject(str);
2.String转JSONArray
String jsonMessage = "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +
"{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +
"{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";
JSONArray myJsonArray = JSONArray.fromObject(jsonMessage);
System.out.println(myJsonArray);
3.String转Map
(1)json-lib(即net.sf.json.JSONObject)
String jsonMessage = "{\"语⽂\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}";
JSONObject myJson = JSONObject.fromObject(jsonMessage);
Map m = myJson;
(2)⽤阿⾥巴巴的fastjson的jar包
String str = "{\"baid\":null,\"32d3:\":\"null\",433:\"0x32\",032:\"ju9fw\"}";
Map mapTypes = com.alibaba.fastjson.JSON.parseObject(str);
(3) (需要引⼊jackjson的core、databind、annotations三个jar包 )
String json = "{\"PayPal key2\":\"PayPal value2\",\"PayPal key1\":\"PayPal value1\",\"PayPal key3\":\"PayPalvalue3\"}"; ObjectMapper mapper = new ObjectMapper();
Map<String,Object> m = adValue(json, Map.class);
(4)(特殊格式的String)
String a ="{se=2016, format=xml, at=en co=3}";
a = a.substring(1, a.length()-1);
Map docType = new HashMap();
Java.util.StringTokenizer items;
for(StringTokenizer entrys = new StringTokenizer(a, ", ");entrys.hasMoreTokens();
docType.Token(), items.hasMoreTokens() ? ((Object) (Token())) : null)){
items = new Token(), "=");
}
(5)Gson
Map map = new Gson().fromJson(jsonStr,Map.class);
4.JSONObject、JSONArray,Map转String
JSONObject——String:
System.out.println(myJsonObject);//可直接输出JSONObject的内容
阿⾥巴巴fastjson:
String str = JSONString(obj,SerializerFeature.BrowserCompatible);
JSONArray——String:
System.out.println(myJsonArray);//可直接输出myJsonArray的内容
Map——String:
System.out.println(map);//可直接输出map的内容
5.JSONObject转JSONArray
6.JSONObject转Map
JSONObject myJson = JSONObject.fromObject(jsonString);
Map m = myJson;
7.JSONArray转JSONObject
for(int i=0 ; i < myJsonArray.length() ;i++){
//获取每⼀个JsonObject对象
JSONObject myjObject = JSONObject(i);
}
8.JSONArray转Map
先jsonArray先循环取出jsonObject,在转成Map
9.Map转JSONObject
(1)json-lib(即net.sf.json.JSONObject)
JSONObject json = JSONObject.fromObject( map );
或
String jsonString = JSONString(map,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty);(2)alibaba的fastjson
JSONObject json = JSON.parseObject(str);
Map转json格式的String
//需要引⼊alibaba的fastjson包
10.Map转JSONArray
JSONArray.fromObject(map);
11.List转JSONArray
JSONArray jsonArray2 = JSONArray.fromObject( list );
fastjson:List转JSONArray
(1)List<Object> list1 = new ArrayList<Object>();
list1.add("false");
list1.add(true);
list1.add(null);
list1.add(0x13e);
list1.add(0123);
JSONArray array1 = JSONArray.JSONString(list1));
(2)JSONArray jsonArray = new JSONArray(list1) ;
12.JSONArray转List
List<Map<String,Object>> mapListJson = (List)jsonArray;
或
public static List<Map<String, Object>> jsonArrayToList(JSONArray ja){
JavaObject(ja, List.class);
}
jsonArray转实体类list:
Student[] ss =(Student[])net.sf.Array(jsonArray,Student.class);
fastjson:JSONArray转List
JSONArray arr = new JSONArray();
arr.add(0,"13");
arr.add(1,"jo");
arr.add(2,"kpo");
List<String> list = JSONObject.JSONString(), String.class);
JSONArray array = new JSONArray();
List<T> list = JSONObject.JSONString(), T.class);
13.String转数组
String string = "a,b,c";
String [] stringArr= string.split(","); //注意分隔符是需要转译滴...
如果是"abc"这种字符串,就直接
String string = "abc" ;
char [] stringArr = CharArray(); //注意返回值是char数组
如果要返回byte数组就直接使⽤getBytes⽅法就ok了~~
String string = "abc" ;
byte [] stringArr = Bytes();
String转List
String str = "";
List<T> list = JSONObject.parseArray(str, T.class);
或net.sf.json
String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"},{\"name\":\"JSON2\",\"age\":\"24\",\"address\":\"北京市西城区\"}]";
//转化为list
List<Student> list2=(List<Student>)net.sf.List(net.sf.json.JSONArray.fromObject(arrayStr), Student.class);
数组转String
char[] data={a,b,c};
String s=new String(data);
14.java实体类的List转String
JSON与String关系⽐较近,⽽JSON的⼦类JSONArray⼜和List关系⽐较近,故把JSONArray做中介过渡:
import com.alibaba.fastjson.JSONArray;
public String list2str(List<?> list){
JSONArray jsonArray = (JSONArray) JSON(list); //List转JSONArray System.out.String());
JSONString(); //JSONArray⽐较容易转String
}
15.Array、List、Set之间转换:
String[] arr = new String[]{"Tom", "Bob", "Jane"};
//Array转List
List<String> arr2list = Arrays.asList(arr);
//Array转Set
Set<String> arr2set = new HashSet<String>(Arrays.asList(arr));
//List转Array
Object[] list2arr = Array();
//List转Set
Set<String> list2set = new HashSet<>(arr2list);
//Set转Array
Object[] set2arr = Array();
/
/Set转List
List<String> set2list = new ArrayList<>(arr2set);
List<String> list = new ArrayList<String>(new HashSet<String>());//
16.java对象转JsonObject
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("北京市西城区");
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(stu);
或alibaba的fastjson:
JSONObject json = (JSONObject) JSON(stu);
17.java对象转jsonArray
Student stu=new Student();
stu.setName("JSON");
stu.setAge("23");
stu.setAddress("北京市西城区");
net.sf.json.JSONArray array = net.sf.json.JSONArray.fromObject(stu);
但alibaba的fastjson报错:
//JSONArray array = (JSONArray) JSON(stu);//报错
18.JsonObject转java对象
Student stu=(Bean(jsonObject, Student.class);
19.jsonArray转java对象
jsonArray先循环取出jsonObject,再转成java对象
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论