前端之家收集整理的这篇文章主要介绍了
FastJson 进行数据转换的 实力,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.newegg.frontservice.web.aspect; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.annotate.JsonAnyGetter; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class Test { Tom tom=new Tom(); List<Jam> jams = new ArrayList<Jam>(); public static void main(String[] args) { Test test = new Test(); test.build(); } public void build(){ tom.setName("tom"); tom.setId(1); List<String> foods=new ArrayList<String>(); foods.add("food1"); foods.add("food2"); tom.setFoods(foods); Jam jam1 = new Jam(); jam1.setId(11); jam1.setName("jam1"); foods=new ArrayList<String>(); foods.add("food1"); foods.add("food2"); foods.add("food3"); jam1.setFoods(foods); Jam jam2 = new Jam(); jam2.setId(12); jam2.setName("jam2"); foods=new ArrayList<String>(); foods.add("food2"); foods.add("food3"); foods.add("food4"); jam2.setFoods(foods); jams.add(jam1); jams.add(jam2); test(JSON.par
SEObject(JSONObject.toJSONString((tom))),JSON.parseArray(JSON.toJSONString(jams))); } public void test(JSONObject object,JSONArray array){ System.out.println(object); System.out.println(array); JSONArray arr = (JSONArray)object.get("foods"); for (int i = 0; i < array.size(); i++) { for (int j = 0; j < arr.size(); j++) { if(array.getJSONObject(i).getJSONArray("foods").contains(arr.get(j))){ System.out.println("it contain "+arr.get(j)); break; } } } } } class Tom{ private int id; private String name; private List<String> foods; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getFoods() { return foods; } public void setFoods(List<String> foods) { this.foods = foods; } } class Jam{ private int id; private String name; private List<String> foods; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getFoods() { return foods; } public void setFoods(List<String> foods) { this.foods = foods; } }