public static void readDataFromJson(){
//读取json文件,保存到String json中
String fileName="c:/temp/data.json";
File file=new File(fileName);
StringBuffer sb = new StringBuffer() ;
String sJson;
String line;
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((line=br.readLine())!=null){
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String json=sb.toString();
// System.out.println("json:"+json);
//利用com.google.gson 包将json转换成List
List persons=new ArrayList();
Gson gson = new Gson();
List<JsonElement> list=new ArrayList();
JsonParser jsonParser=new JsonParser();
JsonElement jsonElement=jsonParser.parse(json); //将json字符串转换成JsonElement
JsonArray jsonArray=jsonElement.getAsJsonArray(); //将JsonElement转换成JsonArray
Iterator it=jsonArray.iterator(); //Iterator处理
while(it.
hasNext()){ //循环
jsonElement=(JsonElement) it.next(); //提取JsonElement
json=jsonElement.toString(); //JsonElement转换成String
Person person=gson.fromJson(json, Person.class); //String转化成JavaBean
persons.add(person); //加入List
}
System.out.println("ok");
}