【问题标题】:how to deal with json name field that's dynamically generated?如何处理动态生成的 json 名称字段?
【发布时间】:2018-06-20 19:18:31
【问题描述】:

例如: https://store.steampowered.com/api/appdetails/?appids=435150&filters=basic

name 是 435150,其值为 successdata。 如果我请求另一场比赛,name 会更改。假设是 578080。

https://store.steampowered.com/api/appdetails/?appids=578080&filters=basic

 public void readJson(String gameID) throws IOException {
        String targetURL = String.format(STEAM_API, gameID);
        URL url = new URL(targetURL);
        InputStreamReader reader = new InputStreamReader(url.openStream());
        ObjectMapper mapper = new ObjectMapper();
        SteamResponseWrapper wrapper = mapper.readValue(reader, SteamResponseWrapper.class);
        System.out.println(wrapper.getSteamResponse().getGame().toString());
    }

public class SteamResponseWrapper {
      private String gameID;
      private SteamResponse steamResponse;
    }

public class SteamResponse {
      private boolean success;
      private Game game;
    }

public class Game {
      private String name;
      private int steam_appid;
    }

我收到此错误原因:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“435150”(util.SteamResponseWrapper 类),未标记为可忽略(2 个已知属性:“steamResponse”, "游戏ID"])

【问题讨论】:

  • 可以修改json的格式吗?
  • 您可以标记您的类以忽略反序列化时的未知字段并继续处理。除非您真的想要/需要处理此特定字段,否则请使用 Map

标签: java json


【解决方案1】:

如何先将其读取到 Map,然后使用带有 successdata 属性的 Wrapper 获取值和解析?

看看this

【讨论】:

    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多