当前位置:首页 » 安卓系统 » 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 浏览:874
阿杜访问 发布:2024-10-05 22:44:23 浏览:603
我的世界怎么在别的服务器开挂 发布:2024-10-05 22:31:14 浏览:297
下沉算法 发布:2024-10-05 21:59:43 浏览:1000