當前位置:首頁 » 安卓系統 » androidjson轉list

androidjson轉list

發布時間: 2024-07-07 06:36:36

① android如何將list轉化為json

可以通過jsonObject作為中間轉換橋梁,先把你的list轉換為jsonObject,然後jsonObject toString(),即可轉換為json字元串。但是過於復雜的map對象貌似不行,這樣你就只有自己寫了

② 濡備綍鍙栧嚭Json閲岄潰鐨勬暟緇

闇瑕佸啓涓涓鏂規硶錛屾妸json鏁版嵁杞鎹㈡垚list闆嗗悎鏁版嵁
public static List jsonToBean(String data, Object bean) {
List list = new ArrayList();

try {
JSONArray array;

try {
array = new JSONArray(data);
for (int i = 0; i < array.length(); i++) {
Object toBean = getBean(bean);
JSONObject ob = new JSONObject();
ob = (JSONObject) array.get(i);
toBean = jsonStrToBean(ob, toBean);
list.add(toBean);

}
return list;
} catch (JSONException e) {
try {
Object obj = null;
JSONObject jsonObj = new JSONObject(data);
Object toBean = getBean(bean);
toBean = jsonStrToBean(jsonObj, toBean);
list.add(toBean);
return list;
} catch (JSONException e1) {
log.error("Error covert String to JSONObject", e);
e1.printStackTrace();
}
e.printStackTrace();
log.error("Error covert String to JSONArray", e);
}

} catch (SecurityException e) {
e.printStackTrace();
}
return list;
}
鐒跺悗鍙栧嚭list闆嗗悎鏁版嵁涓鐨勫煎簲璇ユ病闂棰樹簡鍚с

③ 鎬庢牱鐢–#鎶奐SON鏁版嵁杞鎹涓簂ist鎴栨暟緇

寮曠敤錛屽湪欏圭洰涓寮曠敤
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
==================
瀹氫竴涓璺烰son涓鏍風殑綾
public class CJsonObj
{
//灞炴
}
浣犵殑Json鏄鏂囦歡錛屽氨鎶婅繖鏂囦歡璇誨埌嫻侀噷闈㈡潵鍐嶈漿
string jsonText = System.IO..ReadAllText(Json鏂囦歡); //榪欏氨璇誨嚭浜嗘枃浠訛紝騫跺瓨鎴愪簡string
List<CJsonObj> _Test = new List<CJsonObj>(); //寤虹珛list
//榪欎釜鏄鍏抽敭瀵硅薄DataContractJsonSerializer
DataContractJsonSerializer _Json = new DataContractJsonSerializer(_Test.GetType());
//杞鐮侊紝鎶妔tring 杞鎴恇yte[]鏁扮粍
byte[] _Using = System.Text.Encoding.UTF8.GetBytes(_JsonText);
//寮涓嫻侊紝鎶婃暟緇勫姞榪涘幓
System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using);
_MemoryStream.Position = 0; //嫻佷粠0寮濮
//灝辮繖鍙ワ紝璇誨埌List閲屼簡
_Test = (List<Test>)_Json.ReadObject(_MemoryStream);

//浣犵殑JSON鏂囦歡鏍煎紡蹇呴』涓庝綘瀹氱殑綾諱竴鑷達紝浣犳噦寰

熱點內容
照片視頻加密 發布:2024-10-05 23:58:58 瀏覽:479
北京java培訓班多少錢 發布:2024-10-05 23:49:03 瀏覽:814
subversion源碼安裝 發布:2024-10-05 23:48:17 瀏覽:122
ipad文件怎麼解壓縮 發布:2024-10-05 23:06:28 瀏覽:166
存儲伺服器主控晶元 發布:2024-10-05 23:04:33 瀏覽:572
php學徒 發布:2024-10-05 23:04:30 瀏覽:441
活字格手機端清除緩存了什麼辦 發布:2024-10-05 23:03:23 瀏覽:873
阿杜訪問 發布:2024-10-05 22:44:23 瀏覽:603
我的世界怎麼在別的伺服器開掛 發布:2024-10-05 22:31:14 瀏覽:297
下沉演算法 發布:2024-10-05 21:59:43 瀏覽:1000