androidjson轉list
① 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鏂囦歡鏍煎紡蹇呴』涓庝綘瀹氱殑綾諱竴鑷達紝浣犳噦寰