package com.josn.demo; import java.io.FileNotFoundException; import java.io.FileReader; import com.google.gson.JsonArray; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; /** * http://www.cnblogs.com/zhanglu-cape/p/3960029.html gson介绍 * http://www.cnblogs.com/javaee6/p/3714766.html jackjson 使用介绍 */ public class ReadJSON { public static void main(String args[]) throws FileNotFoundException{ String test = "{"cat":"it","language":[{"id":1,"ide":"eclipse","name":Java},{"id":2,"ide":"XCode","name":"Swift"}, {"id":3,"ide":"Visual Stdio","name":"C#"} ],"pop":true}"; try { /**这里使用的是 gson的解析器 去解析 * dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.1</version> </dependency> */ JsonParser parser=new JsonParser(); //创建JSON解析器 //JsonObject object=(JsonObject) parser.parse(new FileReader("test.json")); //创建JsonObject对象 JsonObject object=(JsonObject) parser.parse(test); System.out.println("cat="+object.get("cat").getAsString()); //将json数据转为为String型的数据 System.out.println("pop="+object.get("pop").getAsBoolean()); //将json数据转为为boolean型的数据 JsonArray array=object.get("language").getAsJsonArray(); //得到为json的数组 for(int i=0;i<array.size();i++){ System.out.println("---------------"); JsonObject subObject=array.get(i).getAsJsonObject(); System.out.println("id="+subObject.get("id").getAsInt()); System.out.println("name="+subObject.get("name").getAsString()); System.out.println("ide="+subObject.get("ide").getAsString()); } } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } } }
链接:http://blog.csdn.net/levae1024/article/details/78610566 json转换之 -- jsonBinder