微微一笑满城空

导航

json与list之间的转换

package com.listandjson;

import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;

public class Test {



public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("123");

//list转成json
String json =JSONArray.fromObject(list).toString();
System.out.println(json);
//运行:["abc","123"]

//json转成list
JSONArray jsonArray = JSONArray.fromObject(json);
List<String> list2 = (List) JSONArray.toCollection(jsonArray);
for (int i = 0; i < list2.size(); i++) {
System.out.println(list2.get(i));
//运行:abc
//   123
}
}
}

posted on 2017-09-08 16:02  微微一笑满城空  阅读(1218)  评论(0编辑  收藏  举报