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鏂囦欢镙煎纺蹇呴’涓庝綘瀹氱殑绫讳竴镊达纴浣犳哕寰