JAVA中常用的格式转换

来源:互联网 发布:vb施工管理 编辑:程序博客网 时间:2024/06/01 09:33
1、String转JSONObject
String jsonMessage = "{\"id\":\"123456\",\"name\":\"小名\",\"age\":\"18\"}";

JSONObject  myJson = JSONObject.fromObject(jsonMessage);


2、String转JSONArray
String jsonMessage = "[{'aa':'11','bb':'22','cc':'33'}]";
JSONArray myJsonArray = JSONArray.fromObject(jsonMessage);

3、JSONObject转String:
JSONObject.toString();


4、JSONArray转String:
JSONArray.toString();


5、Map转String:
map.toString();


6、JSONArray转JSONObject
  for(int i=0 ; i < myJsonArray.size() ;i++)
   {
    //获取每一个JsonObject对象
    JSONObject myjObject = myJsonArray.getJSONObject(i);
}


7、Map转JSONObject
JSONObject json = JSONObject.fromObject( map );   


8、Map转JSONArray
   JSONArray.fromObject(map);


9、List转JSONArray
JSONArray jsonArray2 = JSONArray.fromObject( list );    

注:list是List<map>集合


10、JSONArray转List
List<Map<String,Object>> mapListJson = (List)jsonArray;


11、String转Map
String a = "{\"id\":\"123456\",\"name\":\"小名\",\"age\":\"18\"}";
JSONObject  myJson = JSONObject.fromObject(a);
Map m = myJson; 


12、数组转String
char[] data={a,b,c}; 
String s=new String(data); 


13、String 转换成整数 int

 (1)、. int i = Integer.parseInt([String]); 或
       i = Integer.parseInt([String],[int radix]);
  (2)、int i = Integer.valueOf(my_str).intValue();


14、 int 转换成字串 String

(1)、 String s = String.valueOf(i);
(2)、 String s = Integer.toString(i);
(3)、 String s = "" + i;


15、 字符串转换成日期类型: 

(1)、  Date date=new Date("2008-04-14");  
(2)、  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
String dstr="2008-4-24";  
java.util.Date date=sdf.parse(dstr);  


16、日期转换成字符串:  
  
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
java.util.Date date=new java.util.Date();  
String str=sdf.format(date);  

0 0
原创粉丝点击