Android开发将List转化为JsonArray和JsonObject
来源:互联网 发布:mysql nosql 编辑:程序博客网 时间:2024/06/07 08:17
客户端需要将List<Object>转化为JsonArray和JsonObject的方法:
首先,List中的Object的属性需要是public:
class Person
{
public String name;
public String sex;
public int age;
}
下面假设有List<Person> personList = new ArrayList<Person>(); 中已经装载好了数据:
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
JSONObject tmpObj = null;
int count = personList.size();
for(int i = 0; i < count; i++)
{
tmpObj = new JSONObject();
tmpObj.put("name" , personList.get(i).name);
tmpObj.put("sex", personList.get(i).sex);
tmpObj.put("age", personList.get(i).age);
jsonArray.put(tmpObj);
tmpObj = null;
}
String personInfos = jsonArray.toString(); // 将JSONArray转换得到String
jsonObject.put("personInfos" , personInfos); // 获得JSONObject的String
jsonArray转换的String如下:
[{"name": "张三", "sex": "男", "age": 22}, {"name": "李四", "sex": "男", "age": 23}, {"name": "杨丹", "sex": "女", "age": 18}]
jsonObject转化的String如下:
{"personInfos": [{"name": "张三", "sex": "男", "age": 22}, {"name": "李四", "sex": "男", "age": 23}, {"name": "杨丹", "sex": "女", "age": 18}]}
- Android开发将List转化为JsonArray和JsonObject
- Android开发将List转化为JsonArray和JsonObject
- Android开发将List转化为JsonArray和JsonObject
- List<JSONObject>和JSONArray
- java 将jsonarray 转化为对应键值的jsonobject
- 如何通过Java代码将JsonArray转化为list集合
- JSONArray和JSONObject到java 原生Map和List的深层转化
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray .
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- json字符串转换为JSONObject和JSONArray
- 表格demo
- c c++ 在linux上编译
- 文件上传工具--dropzone.js
- 经典的机器学习方面源代码库 转载备用
- SpringMVC刘宝宝深度版
- Android开发将List转化为JsonArray和JsonObject
- IOC初认识
- iOS之网络—— NSURLSessionDataTask文件离线断点下载、NSURLSession文件上传、AFN基本使用、Cocoapods安装
- contos firewalld 打开或关闭端口
- 关于PHP程序员解决问题的能力
- Android 可以在控件中直接使用的椭圆
- uoj35: 后缀排序
- html5 app开发,你知道多少?
- 在sublime Text3 下使用ctags