Android中 String 和 Uri类型和 Json的转换

1,String ---->  Uri

Uri uri = Uri.parse((String) str);

例如: str = “content://*******”

str = "file://**********"

str = "http://*************"

str = "ftp://***********"


2,Uri   ----->  String

String str = uri.toString();




 GSON 是一个 Java 类库,可以把 Java 对象转换成 JSON 数据,也可以把 JSON 字符串转换成 Java 对象,支持任意复杂的 

Java 对象,包括没有源代码的对象。


  1. public class GsonTools {  
  2.   
  3.     public GsonTools() {  
  4.     }  
  5.   
  6.     public static <T> T getPerson(String jsonString, Class<T> cls) {  
  7.         T t = null;  
  8.         try {  
  9.             Gson gson = new Gson();  
  10.             t = gson.fromJson(jsonString, cls);  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         return t;  
  15.     }  
  16.   
  17.     public static <T> List<T> getPersons(String jsonString, Class<T> cls) {  
  18.         List<T> list = new ArrayList<T>();  
  19.         try {  
  20.             Gson gson = new Gson();  
  21.             list = gson.fromJson(jsonString, new TypeToken<List<T>>() {  
  22.             }.getType());  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return list;  
  27.     }  
  28.   
  29.     public static List<String> getList(String jsonString) {  
  30.         List<String> list = new ArrayList<String>();  
  31.         try {  
  32.             Gson gson = new Gson();  
  33.             list = gson.fromJson(jsonString, new TypeToken<List<String>>() {  
  34.             }.getType());  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.         return list;  
  39.     }  
  40.   
  41.     public static List<Map<String, Object>> listKeyMaps(String jsonString) {  
  42.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  43.         try {  
  44.             Gson gson = new Gson();  
  45.             list = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() {  
  46.             }.getType());  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.         return list;  
  51.     }  
  52.   
  53. }  

Map<String,Object> map = new HashMap<String,Object>();
map.put("sex", type);
map.put("marriage", marriage);
map.put("education", education);
map.put("origin", locationStr);
map.put("address", nowAddressStr + "" + detailsAddressStr);
map.put("contact1", contact1);
map.put("contact1Name", nameStr);
map.put("contact1Phone", phoneStr);
map.put("contact2", contact2);
map.put("contact2Name", nameDownStr);
map.put("contact2Phone", phoneDownStr);
map.put("personalProperty", personalProperty);
map.put("vocation", vocationStr);

Gson gson=new Gson();
String s = gson.toJson(map);



 GSON 是一个 Java 类库,可以把 Java 对象转换成 JSON 数据,也可以把 JSON 字符串转换成 Java 对象,支持任意复杂的 

Java 对象,包括没有源代码的对象。


  1. public class GsonTools {  
  2.   
  3.     public GsonTools() {  
  4.     }  
  5.   
  6.     public static <T> T getPerson(String jsonString, Class<T> cls) {  
  7.         T t = null;  
  8.         try {  
  9.             Gson gson = new Gson();  
  10.             t = gson.fromJson(jsonString, cls);  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         return t;  
  15.     }  
  16.   
  17.     public static <T> List<T> getPersons(String jsonString, Class<T> cls) {  
  18.         List<T> list = new ArrayList<T>();  
  19.         try {  
  20.             Gson gson = new Gson();  
  21.             list = gson.fromJson(jsonString, new TypeToken<List<T>>() {  
  22.             }.getType());  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         return list;  
  27.     }  
  28.   
  29.     public static List<String> getList(String jsonString) {  
  30.         List<String> list = new ArrayList<String>();  
  31.         try {  
  32.             Gson gson = new Gson();  
  33.             list = gson.fromJson(jsonString, new TypeToken<List<String>>() {  
  34.             }.getType());  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.         return list;  
  39.     }  
  40.   
  41.     public static List<Map<String, Object>> listKeyMaps(String jsonString) {  
  42.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  43.         try {  
  44.             Gson gson = new Gson();  
  45.             list = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() {  
  46.             }.getType());  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.         return list;  
  51.     }  
  52.   
  53. }  

Map<String,Object> map = new HashMap<String,Object>();
map.put("sex", type);
map.put("marriage", marriage);
map.put("education", education);
map.put("origin", locationStr);
map.put("address", nowAddressStr + "" + detailsAddressStr);
map.put("contact1", contact1);
map.put("contact1Name", nameStr);
map.put("contact1Phone", phoneStr);
map.put("contact2", contact2);
map.put("contact2Name", nameDownStr);
map.put("contact2Phone", phoneDownStr);
map.put("personalProperty", personalProperty);
map.put("vocation", vocationStr);

Gson gson=new Gson();
String s = gson.toJson(map);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个小狼娃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值