关于list转换json
2017-02-03 14:21
232 查看
上传类似{"device_id":"8bae9c73330c3cee","user_id":"141","ios_lang_key":"en","person":[{"email":"201677577@qq.com","first_name":"nick","last_name":"nick","phone":"1"},
{"email":"201677577@qq.com","first_name":"forrey","last_name":"forrey","phone":"1"},{"email":"201677577@qq.com","first_name":"forrey","last_name":"forrey","phone":"1"}]}的报文
private void tojson() {
List<Person> list = new ArrayList<Person>();
Person p = new Person("201677577@qq.com","forrey","forrey","1");
JSONObject js = new JSONObject();
list.add(p);
p = new Person("201677577@qq.com","nick","nick","2");
list.add(p);
p = new Person("201677577@qq.com","forrey","forrey","3");
list.add(p);
Gson gs = new Gson();
JSONObject object=new JSONObject();
JSONArray jsonArray=new JSONArray();
try {
for(int i = 0; i < list.size() - 1 ; i ++ )
{
js.put("email",list.get(i).getEmail());
js.put("first_name",list.get(i).getFirst_name());
js.put("last_name",list.get(i).getLast_name());
js.put("phone",list.get(i).getPhone());
jsonArray.put(js);
}
object.put("person", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
{"email":"201677577@qq.com","first_name":"forrey","last_name":"forrey","phone":"1"},{"email":"201677577@qq.com","first_name":"forrey","last_name":"forrey","phone":"1"}]}的报文
private void tojson() {
List<Person> list = new ArrayList<Person>();
Person p = new Person("201677577@qq.com","forrey","forrey","1");
JSONObject js = new JSONObject();
list.add(p);
p = new Person("201677577@qq.com","nick","nick","2");
list.add(p);
p = new Person("201677577@qq.com","forrey","forrey","3");
list.add(p);
Gson gs = new Gson();
JSONObject object=new JSONObject();
JSONArray jsonArray=new JSONArray();
try {
for(int i = 0; i < list.size() - 1 ; i ++ )
{
js.put("email",list.get(i).getEmail());
js.put("first_name",list.get(i).getFirst_name());
js.put("last_name",list.get(i).getLast_name());
js.put("phone",list.get(i).getPhone());
jsonArray.put(js);
}
object.put("person", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
相关文章推荐
- 关于list转换成json报错和小实例
- 关于FastJson中jsonArray转换成list集合的方法
- 关于使用ajax请求servletList转换Json抛的异常There is a cycle in the hierarchy!暨将List转换为JSONArray问题解决!
- list转json的时候,关于date转换的问题
- 关于FastJson中jsonArray转换成list集合的方法
- 关于json格式存储数据库成bean对象list,取出来再次转换成json格式
- c# Json List DataTable 数据相互转换
- 关于数组和List之间相互转换的方法
- [java]关于数组和List之间相互转换的方法
- 关于把JavaBean转换成List的方法
- python-json-javascript之间关于字符集的转换问题
- 关于PHP在数组对象与JSON数据之间的转换问题(编码问题)
- 关于数组和List之间相互转换的方法
- 将DataTable或Ilist转换成JSON格式
- c# 任意类型转换成json (datable,list,array 等)
- 关于JSON与JAVA数据的转换
- 将DataTable或Ilist<>转换成JSON格式
- 将DataTable或Ilist<>转换成JSON格式
- 关于数组和List之间相互转换的方法
- 关于父类与子类的类型转换(TStrings与TStringList)