public class a11111111 { //参数obj可以是 json对象,字符串, list public static void fun(Object obj){ JSONArray data = JSONArray.fromObject(obj); for(int i=0;i<data.size();i++){ JSONObject jobj = (JSONObject) data.get(i); System.out.println(jobj); /** *{"aid":3201,"name":"品牌","cid":"1351"} {"aid":7716,"name":"类型","cid":"1351"} {"aid":8668,"name":"价格","cid":"1351"} */ String name = (String)jobj.get("name"); System.out.println(name); // 品牌 类型 价格 } } public static void main(String[] args) {
//制作string
//String list = request.getParameter("json"); String str9 = " [{"aid":3201,"name":"品牌","cid":"1351"}, {"aid":7716,"name":"类型","cid":"1351"}, {"aid":8668,"name":"价格","cid":"1351"}] " ; fun(str9); // 制作一个list List<Object> list90 = new ArrayList<Object>(); JSONArray data = JSONArray.fromObject(str9); for(int i=0;i<data.size();i++){ JSONObject jobj = (JSONObject) data.get(i); list90.add(jobj); // 将一个json数组转成list /** *{"aid":3201,"name":"品牌","cid":"1351"} {"aid":7716,"name":"类型","cid":"1351"} {"aid":8668,"name":"价格","cid":"1351"} */ } fun(list90); //调用的2个函数结果都是一样的,一样的 String str_0 = list90.toString(); fun(str_0); //结果都是一样的 } }