Json串中的转义字符解析
⼀:解析普通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:"+("currentTime"));
System.out.println("direction:"+("direction"));
System.out.println("latitude:"+("latitude"));
System.out.println("longitude:"+("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 = String("message");
System.out.println("message:"+jsonstrtemp);
jsonObject = new JSONObject(jsonstrtemp);
System.out.println("currentTime:"+("currentTime"));
System.out.println("direction:"+("direction"));
System.out.println("latitude:"+("latitude"));
System.out.println("longitude:"+("longitude"));
⼆:遍历Json对象
JSONObject ports = ja.getJSONObject("ports");
Iterator<String> keys = ports.keys();
while(keys.hasNext()){
String ();
String value = String(key);
}
三:使⽤Gjson,json与对象相互转化
使⽤Gson轻松将java对象转化为json格式
String json = Json(Object);//得到json形式的字符串
User user = gson.fromJson(json,User.class);//得到对象
转化成list
import java.util.List;
le.gson.Gson;
flect.TypeToken;
import com.lc.function.Action;
import dels.Groups;
public class MapSearch {
private void ParseData(String _data)
{
Gson gson = new Gson();
List<Groups> ps = gson.fromJson(_data, new TypeToken<List<Groups>>(){}.getType()); System.out.(0).getGroup_name());json转换对象
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。