java Json 串中的转义字符

2025-03-06 13:00:10
推荐回答(5个)
回答1:

一:解析普通json

1:不带转化字符

格式{"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"));

jsonarray
JSONObject 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);//得到对象

转化成list
import 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());
}
}

回答2:

一:解析普通json
1:不带转化字符
格式{"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"));

jsonarray

JSONObject 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);//得到对象

回答3:

带有转义字符的java 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.optString("message");

解析获取json字符串里的值时,建议用jsonObject.optString的方式,它相较于传统的

jsonObject.getString方式 在得不到你想要的值时候返回空字符串, 而getString的方式

会抛出异常

回答4:

没办法,用工具一次就加好。比如说Notepad++ 等
用替换的方式一下子弄好,还支持部分正则表达式

你觉得eclipse能辨别你那个双引号需要转义哪些不用转义,怎么可能这么智能。

回答5:

需要添加的。。。。。。。。在 notepad++ 那样的环境,使用批量替换 ,更快捷