本文目錄一覽:
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:”+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”);
IteratorString 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();
ListGroups ps = gson.fromJson(_data, new TypeTokenListGroups(){}.getType());
System.out.println(ps.get(0).getGroup_name());
}
}
小程序如何使用json文件
【小程序】小程序讀取本地json文件
1.在項目中新建data文件夾內新建存放json數據的js文件;
2.在存放json數據的js文件中定義出口(其中出口名稱為regionList,數據名為json);
3.頁面引用:
1、let jsonData = require(‘../../data/region.js’);
2、onload中給數據傳入
_this.setData({
multiArray: jsonData.regionList[0].multiArray,
objectMultiArray: jsonData.regionList[0].objectMultiArray
});
json數據怎麼加上轉義字元
首先你的轉義範圍要先確定吧?
一般都是對中文、符號進行轉義;
中文一般都是轉成unicode;
如果數要轉換成程序(JAVA)中直接使用的json字元串的話,一般都是在雙引號前加\.
推薦給你一個json在線轉義工具 網頁鏈接 可以去看下。
原創文章,作者:AYCQU,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/329751.html