java json串去转义符_java Json 串中的转义字符?

a7806f104b6e96ddec4a1069d68fd7e0.png

智慧大石

一:解析普通json1:不带转化字符格式{"type":"ONLINE_SHIPS","message":{"currentTime":1400077615368,"direction":0,"id":1,"latitude":29.5506,"longitude":106.6466}}JSONObject jsonObject = new JSONObject(jsonstr).getJSONObject("message");System.out.println("currentTime:"+jsonObject.get("currentTime"));System.out.println("direction:"+jsonObject.get("direction"));System.out.println("latitude:"+jsonObject.get("latitude"));System.out.println("longitude:"+jsonObject.get("longitude"));jsonarrayJSONObject jo = ja.getJSONArray("cargoList").getJSONObject(0);2:带转义字符的json格式{"type":"ONLINE_SHIPS","message":"{\"currentTime\":1400077615368,\"direction\":0,\"id\":1,\"latitude\":29.5506,\"longitude\":106.6466}"}其实也很简单,先把它转化成字符串就可以了JSONObject jsonObject = new JSONObject(jsonstr);//先通过字符串的方式得到,转义字符自然会被转化掉String jsonstrtemp = jsonObject.getString("message");System.out.println("message:"+jsonstrtemp);jsonObject = new JSONObject(jsonstrtemp);System.out.println("currentTime:"+jsonObject.get("currentTime"));System.out.println("direction:"+jsonObject.get("direction"));System.out.println("latitude:"+jsonObject.get("latitude"));System.out.println("longitude:"+jsonObject.get("longitude"));二:遍历Json对象JSONObject ports = ja.getJSONObject("ports");Iterator keys = ports.keys();while(keys.hasNext()){String key=keys.next();String value = ports.getString(key);}三:使用Gjson,json与对象相互转化使用Gson轻松将java对象转化为json格式String json = gson.toJson(Object);//得到json形式的字符串User user = gson.fromJson(json,User.class);//得到对象转化成listimport java.util.List;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.lc.function.Action;import com.lc.models.Groups;public class MapSearch {private void ParseData(String _data){Gson gson = new Gson();List ps = gson.fromJson(_data, new TypeToken>(){}.getType());System.out.println(ps.get(0).getGroup_name());}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值