标签:[] lan for tst inf main gson otf 字符串
使用JsonParser解析器来解析字符串和输入流,变成json对象
代码如下:
public class Readjson { public static void main(String[] args) { try { JsonParser parser=new JsonParser(); JsonObject object=(JsonObject) parser.parse(new FileReader("test.json")); System.out.println(object.get("cat").getAsString()); System.out.println(object.get("pop").getAsBoolean()); JsonArray languages=object.get("language").getAsJsonArray(); for(int i=0;i<languages.size();i++){ System.out.println("*************"); JsonObject lan=languages.get(i).getAsJsonObject(); System.out.println("id="+lan.get("id").getAsInt()); System.out.println("name="+lan.get("name").getAsString()); System.out.println("ide="+lan.get("ide").getAsString()); } } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
标签:[] lan for tst inf main gson otf 字符串
原文地址:https://www.cnblogs.com/xtuxiongda/p/9062090.html