gson笔记 解析json数据

gson中负责json数据解析的类是JsonReader。

Json格式有两种结构,一种是对象(键值对的组合,无序),另外一种是数组(值的有序集合)。

因此针对这两种格式,JsonReader提供了不同的处理方法。

针对对象

{
  "isRemote":false,
  "remote":{
    "hubURL":"http://127.0.0.1:4444/wd/hub/",
    "browser":"firefox",
    "os":"WINDOWS"
  },
  "local":{
    "browser":"webdriver.firefox.bin",
    "path":"D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"
  }

}

下面读取json对象的内容。

private Boolean isRemote = false;
private LocalBrowser lcBrowser;
private RemoteBrowser rmBrowser;

private void readConf(InputStream in) throws IOException{

  JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

  try{
    reader.beginObject();  //表示开始读入对象,此时指向的是最外层的对象

    while(reader.hasNext()){  //判断当前对象是否仍有属性存在
      String name = reader.nextName();  //如有则获得属性名
      if(name.equals("isRemote")){
        isRemote = reader.nextBoolean();
      }else if(name.equals("remote")){
        rmBrowser=readRemote(reader); 
      }else if(name.equals("local")){
        lcBrowser = readLocal(reader);
      }else{
        reader.skipValue(); //跳过不处理这个值

    reader.endObject(); //表示对象读取结束
  }finally{
    reader.close(); //关闭reader
  }

}

private RemoteBrowser readRemote(JsonReader reader) throws IOException{
  String hubURL = null;
  String browser = null;
  String os =null;

  reader.beginObject(); //这时指向的是remote属性对应的对象
  while(reader.hasNext()){
    String name = reader.nextName();
    if(name.equals("hubURL")){
      hubURL = reader.nextString();
    }else if(name.equals("browser")){
      browser = reader.nextString();
    }else if(name.equals("os")){
      os = reader.nextString();
    }else{
      reader.skipValue();
    }
  }
  reader.endObject();

  return new RemoteBrowser(hubURL, browser, os);

}

private LocalBrowser readLocal(JsonReader reader) throws IOException{
  String browser = null;
  String path = null;

  reader.beginObject();
  while(reader.hasNext()){
    String name = reader.nextName();
    if(name.equals("browser")){
      browser = reader.nextString();
    }else if(name.equals("path")){
      path = reader.nextString();
    }else{
      reader.skipValue();
    }
  }
  reader.endObject();

  return new LocalBrowser(browser, path);
}

 针对数组

[
  {
    "id": 912345678901,
    "text": "How do I read a JSON stream in Java?",
    "geo": null,
    "user": {
      "name": "json_newb",
      "followers_count": 41
    }
  },
  {
    "id": 912345678902,
    "text": "@json_newb just use JsonReader!",
    "geo": [50.454722, -104.606667],
    "user": {
      "name": "jesse",
      "followers_count": 2
    }
  }
]


public List<Message> readJsonStream(InputStream in) throws IOException {
  JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
  try {
    return readMessagesArray(reader);
  } finally {
    reader.close();
  }
}

public List<Message> readMessagesArray(JsonReader reader) throws IOException {
  List<Message> messages = new ArrayList<Message>();

  reader.beginArray(); //开始读取数组,此时指向的是最外层的数组
  while (reader.hasNext()) {
    messages.add(readMessage(reader));//读取数组元素
  }
  reader.endArray();
    return messages;
}

参考链接:
json介绍
Gson
上一篇:Mysql数据库设置定时任务


下一篇:Codeforces Round #378 (Div. 2) D. Kostya the Sculptor map+pair