GSON:使用fromJson转换字典列表

时间:2017-11-05 16:24:35

标签: java json rest api gson

我目前正试图提取“我曾经历过的最好的名字”这个名字。从下面显示的last.fm API使用GSON,但很难不断返回空值。

********* EDIT **************** *************

这是JSON,其中track是一个词典列表,每个歌曲名称都有一个:

{
toptracks: {
   track: [
        {
          name: "Best I Ever Had"
        }
   ]
}
}

使用http://www.jsonschema2pojo.org/我创建了以下类:

TrackName.java

package com.webservice1;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class TrackName {

@SerializedName("toptracks")
@Expose
private Toptracks toptracks;

public Toptracks getToptracks() {
    return toptracks;
}

public void setToptracks(Toptracks toptracks) {
    this.toptracks = toptracks;
}

public TrackName withToptracks(Toptracks toptracks) {
    this.toptracks = toptracks;
    return this;
}

}

Toptracks.java

package com.webservice1;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Toptracks {

@SerializedName("track")
@Expose
private List<Track> track = null;

public List<Track> getTrack() {
    return track;
}

public void setTrack(List<Track> track) {
    this.track = track;
}

public Toptracks withTrack(List<Track> track) {
    this.track = track;
    return this;
}

}

最后是Track.java

package com.webservice1;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Track {

@SerializedName("name")
@Expose
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Track withName(String name) {
    this.name = name;
    return this;
}

}

这是我的代码使用fromJson方法,其中reply包含整个JSON并且经常跟踪名称com.webservice1.Toptracks@1b9e1916'

        String reply;

        reply = reader.readLine();

        Gson gson = new Gson();
        TrackName response = gson.fromJson(reply, TrackName.class);

        System.out.println("Track Name " + response.getToptracks());

任何帮助都将不胜感激!!

1 个答案:

答案 0 :(得分:0)

您可以使用Jackson执行此操作,并且我确定有设置可以忽略丢失的字段等...

这是库的maven依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.2</version>
</dependency>
    String json = "{\n"
            + "  \"toptracks\" : {\n"
            + " \"track\": [{\n"
            + "     \"name\": \"Best I Ever Had\",\n"
            + "     \"playcount\": \"3551414\",\n"
            + "     \"listeners\": \"1058277\",\n"
            + "     \"mbid\": \"00bde944-7562-446f-ad0f-3d4bdc86b69f\",\n"
            + "     \"url\": \"https://www.last.fm/music/Drake/_/Best+I+Ever+Had\",\n"
            + "     \"streamable\": \"0\",\n"
            + "     \"artist\": {\n"
            + "         \"name\": \"Drake\",\n"
            + "         \"mbid\": \"b49b81cc-d5b7-4bdd-aadb-385df8de69a6\",\n"
            + "         \"url\": \"https://www.last.fm/music/Drake\"\n"
            + "     }\n"
            + " }]\n"
            + "}\n"
            + "}";

    ObjectMapper mapper = new ObjectMapper();

    TopTracks track = mapper.readValue(json, TopTracks.class);

    List<Object> trackList = (List<Object>)track.toptracks.get("track");

    Map<String, Object> trackMap = (Map<String, Object>) trackList.get(0);

    System.out.println(trackMap.get("name"));

public class TopTracks {

    public Map<String, Object> toptracks = new HashMap<>();
}

输出:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ MVN ---
Best I Ever Had
------------------------------------------------------------------------
BUILD SUCCESS