【问题标题】:Parse a list of JSONs with Gson使用 Gson 解析 JSON 列表
【发布时间】:2015-06-25 22:37:34
【问题描述】:

我正在尝试使用 Gson 解析 JSON 文件,但在尝试获取所有值时遇到了很多问题。 JSON 的结构如下图所示:

如您所见,这是我需要写入文件(每行一个)的推文 json 列表(不是数组,没有括号)。问题是,我如何解析这个 JSON 并分别获取每条推文?

【问题讨论】:

  • 这应该会有所帮助jsonschema2pojo.org
  • org.json图书馆帮你。
  • @Pshemo 这是一个非常酷的工具,但我只需要获取每条推文和一个字符串。
  • 您仍然没有解释是什么阻止了您。你知道如何使用 Gson 的基础知识吗?您是否创建了无法按预期工作的代码(如果是,请发布您的代码并解释您遇到的问题)?
  • 我了解一些基础知识,并且了解您的工具生成的代码,但我不知道如何将整个对象作为字符串而不是作为具有每个值的对象。

标签: java json parsing gson


【解决方案1】:

这样的事情会让你接近。 JsonTestFiles.getJson("tweets.json") 只是将 json 作为字符串返回,这就是您要开始的内容。

@Test    
public void testGson() {
    Gson gson = new Gson();
    Tweets tweets = gson.fromJson(JsonTestFiles.getJson("tweets.json"), Tweets.class);
         System.out.println(tweets);

    }
private static class Tweets {
    public Map<String, Data> id;

    @Override
    public String toString() {
        return "Tweets{" +
                "id=" + id +
                '}';
    }
}

private static class Data {
    public String created_at;
    public int id;
    public String id_str;
    public String text;
    public String source;
    public boolean truncated;
    public String in_reply_to_status_id;

    @Override
    public String toString() {
        return "Data{" +
                "created_at='" + created_at + '\'' +
                ", id=" + id +
                ", id_str='" + id_str + '\'' +
                ", text='" + text + '\'' +
                ", source='" + source + '\'' +
                ", truncated=" + truncated +
                ", in_reply_to_status_id='" + in_reply_to_status_id + '\'' +
                '}';
    }
}

和json:

{
  "id": {
    "50413592" : {
    "created_at" : "Sunday",
    "id" : 50413592,
    "id_str" : "50413592",
    "text" : "Hello text",
    "source":"the source",
    "truncated": false,
    "in_reply_to_status_id": ""
  },
  "50413593" : {
    "created_at" : "Sunday",
    "id" : 50413593,
    "id_str" : "50413593",
    "text" : "Hello text",
    "source":"the source",
    "truncated": false,
    "in_reply_to_status_id": ""
  },
  "50413594" : {
    "created_at" : "Sunday",
    "id" : 50413594,
    "id_str" : "50413594",
    "text" : "Hello text",
    "source":"the source",
    "truncated": false,
    "in_reply_to_status_id": ""
    }
  }
}

【讨论】:

    【解决方案2】:

    我最终这样做了:

    JsonObject jsonObject = parser.parse(line).getAsJsonObject();
    JsonObject o = jsonObject.get("id").getAsJsonObject();
    JsonArray tweets = parser.parse("[" + o.toString() + "]").getAsJsonArray();
    
    for (JsonElement element : tweets) {
        for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
            String tweet = entry.getValue().toString();
        }
    }
    

    这样我就不需要新课程了。不确定这是否是最好的解决方案,但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      相关资源
      最近更新 更多