【问题标题】:Why is my JsonParser returning a null value when I try to extract from it?当我尝试从中提取时,为什么我的 JsonParser 返回一个空值?
【发布时间】:2020-07-31 02:51:51
【问题描述】:

使用 GSON,我试图从一段 JSON 中提取元素,但无论我尝试获取什么值,它们都返回 null,我不知道为什么。我有这段 JSON:

{
   "albums":{
      "href":"https://api.spotify.com/v1/browse/new-releases?offset=0&limit=20",
      "items":[
         {
            "album_type":"album",
            "artists":[
               {
                  "external_urls":{
                     "spotify":"https://open.spotify.com/artist/06HL4z0CvFAxyc27GXpf02"
                  },
                  "href":"https://api.spotify.com/v1/artists/06HL4z0CvFAxyc27GXpf02",
                  "id":"06HL4z0CvFAxyc27GXpf02",
                  "name":"Taylor Swift",
                  "type":"artist",
                  "uri":"spotify:artist:06HL4z0CvFAxyc27GXpf02"
               }
            ],
            "available_markets" : [ "AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA" ],
            "external_urls":{
               "spotify":"https://open.spotify.com/album/2fenSS68JI1h4Fo296JfGr"
            },
            "href":"https://api.spotify.com/v1/albums/2fenSS68JI1h4Fo296JfGr",
            "id":"2fenSS68JI1h4Fo296JfGr",
            "images":[
               {
                  "height":640,
                  "url":"https://i.scdn.co/image/ab67616d0000b27395f754318336a07e85ec59bc",
                  "width":640
               },
               {
                  "height":300,
                  "url":"https://i.scdn.co/image/ab67616d00001e0295f754318336a07e85ec59bc",
                  "width":300
               },
               {
                  "height":64,
                  "url":"https://i.scdn.co/image/ab67616d0000485195f754318336a07e85ec59bc",
                  "width":64
               }
            ],
            "name":"folklore",
            "release_date":"2020-07-24",
            "release_date_precision":"day",
            "total_tracks":16,
            "type":"album",
            "uri":"spotify:album:2fenSS68JI1h4Fo296JfGr"
         }
      ]
   }
}

我试图深入了解我要提取的特定元素,例如“名称”,但这不起作用,所以我调查了原因,它说 jsonArray 为空。我已经尝试提取任何东西,无论我尝试提取什么,它都显示为空。怎么了?

(P.S. 如果我违反了一些发帖规则或什么的,我很抱歉,新手在这里)

public void parseJsonForInfo(String json) {
    JsonObject jo = JsonParser.parseString(json).getAsJsonObject();
    System.out.println(jo);
    JsonArray jsonArray = jo.getAsJsonArray("items");
    System.out.println(jsonArray);   //prints null
}

【问题讨论】:

  • 您必须先选择对象albumsJsonObject albums = jo.getAsJsonObject("albums"); JsonArray jsonArray = albums.getAsJsonArray("items");
  • 这就是解决方案!谢谢:),但是您的评论旁边没有复选标记,我不知道如何将其标记为已解决
  • 评论不是答案,因此您不能接受它,但您可以将其标记为有用。

标签: java json gson


【解决方案1】:

如果您需要选择items,您需要先使用 JSON 对象获取专辑,然后才能使用 JSON 数组获取项目。 这是一个例子

public void parseJsonForInfo(String json) {
    JsonObject jo = JsonParser.parseString(json).getAsJsonObject();
    JsonObject albums = jo.getAsJsonObject("albums");
    JsonArray jsonArray = albums.getAsJsonArray("items");
    System.out.println(jsonArray);   //prints null
}

之后,你会得到你想要的items

【讨论】:

    猜你喜欢
    • 2016-05-13
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多