zoukankan      html  css  js  c++  java
  • Android之Gson

    {

    "id": 912345678901,

    "age": 33, 

    "text": "How do I stream JSON in Java?",

    "geo": null,
     }


    红色部分如果表示有可能为null,则在判断的时候需要如下name.equals("geo")&& reader.peek() != JsonToken.NULL

    橙色部分要是想不解析其中的Json字段,需要在判断的时候加上一个else语句如下:else{reader.skipValue()} 

    然后是一些例子,都是Json的各种组合形式,不定时更新 

    1. 现实Json数据,后面每一个都是如此不再做任何的解析了,然后跟着就是对应的代码内容

     {

         "id": 912345678901,
         "text": "How do I stream JSON in Java?",
         "geo": null,
         "user": {
           "name": "json_newb",
           "followers_count": 41
          }
    }
        reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if (name.equals("id")) {
                        System.out.println("id:" + reader.nextLong());
                    } else if (name.equals("text")) {
                        System.out.println("text:" + reader.nextString());
                    } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
                        System.out.println("text:" + reader.nextString());
                    } else if (name.equals("user")) {
                        usernameObject();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();

    private void usernameObject() throws IOException{
            reader.beginObject();
            while (reader.hasNext()) {
                String nxtName = reader.nextName();
                if (nxtName.equals("name")) {
                    System.out.println("nxtName:" + reader.nextString());
                } else if (nxtName.equals("followers_count")) {
                    System.out.println("nxtName:" + reader.nextInt());
                }

    }  

            

    2.

    [
    {
         "id": 912345678901,
         "text": "How do I stream JSON in Java?",
         "geo": null,
         "user": {
           "name": "json_newb",
           "followers_count": 41
          }
    },
    {
         "id": 777777777888,
         "text": "dfngsdnglnsldfnsl",
         "geo": null,
         "user": {
           "name": "dsfgpd",
           "followers_count": 24
          }
    }

    reader.beginArray();
            while(reader.hasNext()){
                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if (name.equals("id")) {
                        System.out.println("id:" + reader.nextLong());
                    } else if (name.equals("text")) {
                        System.out.println("text:" + reader.nextString());
                    } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
                        System.out.println("text:" + reader.nextString());
                    } else if (name.equals("user")) {
                        usernameObject();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            }

    reader.endArray();  

            

    3. 

    {
    "statuses":[
    {
          "id": 912345678901,
          "text": "How do I stream JSON in Java?",
          "geo": null,
          "user": {
            "name": "json_newb",
            "followers_count": 41
          }
          },
         
    {
          "id": 777777777888,
          "text": "dfngsdnglnsldfnsl",
          "geo": null,
          "user": {
            "name": "dsfgpd",
            "followers_count": 24
          }
          }
         ]

    reader.beginObject();
            while(reader.hasNext())
            { 
                String begin = reader.nextName();
                if(begin.equals("statuses"))
                {
                    array();
                }
            }
            reader.endObject();

    private void array() throws IOException{
            reader.beginArray();
            while(reader.hasNext()){
                reader.beginObject();
                while (reader.hasNext()) {
                    String name = reader.nextName();
                    if (name.equals("id")) {
                        System.out.println("id:" + reader.nextLong());
                    } else if (name.equals("text")) {
                        System.out.println("text:" + reader.nextString());
                    } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
                        System.out.println("text:" + reader.nextString());
                    } else if (name.equals("user")) {
                        usernameObject();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            }
            reader.endArray();
        }

    private void usernameObject() throws IOException{
            reader.beginObject();
            while (reader.hasNext()) {
                String nxtName = reader.nextName();
                if (nxtName.equals("name")) {
                    System.out.println("nxtName:" + reader.nextString());
                } else if (nxtName.equals("followers_count")) {
                    System.out.println("nxtName:" + reader.nextInt());
                }
            }
            reader.endObject();

      }  

      

  • 相关阅读:
    java—在maven项目中配置selenium运行环境
    java—Maven安装配置
    java基础—异常处理
    java基础—IO流
    怎么查看chrome网络日志
    http缓存之304 last-modified,cache-control:max-age,Etag等
    dubbo源码分析7-dubbox怎么对dubbo做了扩展
    SQLite如何测试
    New line
    ElasticSearch 入门笔记1
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2737402.html
Copyright © 2011-2022 走看看