【问题标题】:Extract elements from JSONObject in Android从Android中的JSONObject中提取元素
【发布时间】:2020-10-25 13:42:16
【问题描述】:

我有一个名为 object 的 JSONObject。

它有很多书籍类的对象。

这是第一个元素的打印:

I/System.out: {"id":"1","author":"Paula Hawkins","description":"EVERY DAY THE SAME\n\nRachel   Soon she is deeply entangled not only in the investigation but in the lives of everyone involved. Has she done more harm than good?","publication_date":"13\/01\/2015","title":"The girl on the train","url_image":"hawkins.jpg"}

想获取id、作者等,但不知道怎么获取。

Gson gson = new Gson();
JSONArray mainObject = new JSONArray(total.toString());
for (int i = 0; i < mainObject.length(); i++) {
    JSONObject object = mainObject.getJSONObject(i);
    int id = object.getInt("id");  //This will have the value of the id
    String author = object.getString("author");
    String description = object.getString("description");
    String publication_date = object.getString("publication_date");
    String title = object.getString("title");
    String url_image = object.getString("url_image");

}
System.out.println(mainObject.get(1));

for(int i =0; i<mainObject.length(); i++){
    book = new Book();
}

这是我如何创建对象的代码。

有什么想法吗?

【问题讨论】:

    标签: java android json gson


    【解决方案1】:

    我想通了!。

    System.out.println(mainObject.getJSONObject(1).get("id"));
    

    在这种情况下你得到对象 1 并 .get 你想要得到的属性的名称。

    无论如何,谢谢,我希望这个答案可以帮助其他人!

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多