Json,String,Map之間的轉換

2022-08-04 22:51:22 字數 663 閱讀 4995

前提是string的格式是map或json型別的

string 轉json

jsonobject  jasonobject = jsonobject.fromobject(str);

string 轉map

jsonobject  jasonobject = jsonobject.fromobject(str);

map map = (map)jasonobject;

在網頁之間傳遞資料出現中文亂碼解決方案:

例如請求介面返回乙個map或json資料,包含中文

1,先將map或json轉成string格式,如果是map型別的,先轉成json  jsonobject json=new jsonobject(map); json.tojsonstring();轉成string格式

2,利用urlencoder.encoder(str,"utf-8")或"gbk"對string加密處理,傳送加密後的str

3,在接受端,收到str後,urldecoder.decoder(str,"utf-8")解密成正常str,一般要轉成json需要去掉開始結尾的引號,str=str.substring(1,str.length()-1),再用jsonobject.formobject(str)將str轉成json;之後需要轉map,map map = (map)json即可

Json,String,Map之間的轉換

前提是string的格式是map或json型別的 string 轉json jsonobject jasonobject jsonobject.fromobject str string 轉map jsonobject jasonobject jsonobject.fromobject str ma...

Json,String,Map之間的轉換

前提是string的格式是map或json型別的 string 轉json jsonobject jasonobject jsonobject.fromobject str string 轉map jsonobject jasonobject jsonobject.fromobject str ma...

Json,String,Map之間的轉換

前提是string的格式是map或json型別的 string 轉json jsonobject jasonobject jsonobject.fromobject str string 轉map jsonobject jasonobject jsonobject.fromobject str ma...