使用GSON解析嵌套数据JSON

时间:2017-10-03 18:00:41

标签: android json gson

我已经浏览了一下,虽然我发现了很多类似的问题,但我似乎无法实现在我的方案中有效的解决方案,

我有以下JSON

[
 {
"id": 389161,
"date": "2017-09-27T19:00:35",
"date_gmt": "2017-09-27T17:00:35",
"guid": {
  "rendered": "http://diskizone.com/?post_type=sp_event&p=389161"},
"modified": "2017-09-27T19:41:34",
"modified_gmt": "2017-09-27T17:41:34",
"slug": "baberwa-fc-vs-maccabi-fc",
"status": "publish",
"type": "sp_event",
"link": "http://diskizone.com/event/baberwa-fc-vs-maccabi-fc/",
"title": {
  "rendered": "Baberwa FC vs Maccabi FC"
},
"content": {
  "rendered": "",
  "protected": false
},
"excerpt": {
  "rendered": "",
  "protected": false
},
"author": 1,
"featured_media": 0,
"comment_status": "open",
"ping_status": "closed",
"template": "",
"leagues": [
  212
],
"seasons": [
  817
],
"venues": [
  432
],
"teams": [
  97587,
  6795
],
"main_results": [
  "1",
  "0"
],
"outcome": {
  "6795": "loss",
  "97587": "win"
},
"winner": 97587,
"minutes": 90,
"players": [
  0,
  0
],
"offense": [],
"defense": [],
"staff": [
  0,
  0
],
"results": {
  "0": {
    "goals": "Goals",
    "outcome": "Full time"
  },
  "6795": {
    "goals": "0",
    "outcome": [
      "loss"
    ]
  },
  "97587": {
    "goals": "1",
    "outcome": [
      "win"
    ]
  }
},
"performance": [
  {
    "position": "Position",
    "penaltiessaved": "Penalties Saved",
    "goals": "Goals",
    "assists": "Assists",
    "yellowcards": "Yellow Cards",
    "redcards": "Red Cards",
    "owngoals": "Own Goals"
  }
],
"_links": {
  "self": [
    {
      "href": "http://diskizone.com/wp-json/sportspress/v2/events/389161"
    }
  ],
  "collection": [
    {
      "href": "http://diskizone.com/wp-json/sportspress/v2/events"
    }
  ],
  "about": [
    {
      "href": "http://diskizone.com/wp-json/wp/v2/types/sp_event"
    }
  ],
  "author": [
    {
      "embeddable": true,
      "href": "http://diskizone.com/wp-json/wp/v2/users/1"
    }
  ],
  "replies": [
    {
      "embeddable": true,
      "href": "http://diskizone.com/wp-json/wp/v2/comments?post=389161"
    }
  ],
  "wp:attachment": [
    {
      "href": "http://diskizone.com/wp-json/wp/v2/media?parent=389161"
    }
  ],
  "wp:term": [
    {
      "taxonomy": "sp_league",
      "embeddable": true,
      "href": "http://diskizone.com/wp-json/wp/v2/leagues?post=389161"
    },
    {
      "taxonomy": "sp_season",
      "embeddable": true,
      "href": "http://diskizone.com/wp-json/wp/v2/seasons?post=389161"
    },
    {
      "taxonomy": "sp_venue",
      "embeddable": true,
      "href": "http://diskizone.com/wp-json/wp/v2/venues?post=389161"
    }
  ],
  "curies": [
    {
      "name": "wp",
      "href": "https://api.w.org/{rel}",
      "templated": true
    }
  ]
}
},

...

以及post.java中的以下代码

import java.util.Date;
import java.util.List;


import com.google.gson.annotations.SerializedName;

public class Post {

@SerializedName("id")
Integer ID;

@SerializedName("date")
Date dateCreated;

}

我想知道如何从JSON返回以下信息,我需要将哪些内容添加到post.java才能使其正常工作?

  1. 文字" Baberwa FC vs Maccabi FC"来自" title" > "呈现"

  2. " main_results"所以我可以显示1和0

2 个答案:

答案 0 :(得分:0)

    -----------------------------------com.android.About.java-----------------------------------

    package com.android;


    public class About {

    public String href;

    }
    -----------------------------------com.android.Author.java-----------------------------------

    package com.android;


    public class Author {

    public Boolean embeddable;
    public String href;

    }
    -----------------------------------com.android.Collection.java-----------------------------------

    package com.android;


    public class Collection {

    public String href;

    }
    -----------------------------------com.android.Content.java-----------------------------------

    package com.android;


    public class Content {

    public String rendered;
    public Boolean _protected;

    }
    -----------------------------------com.android.Cury.java-----------------------------------

    package com.android;


    public class Cury {

    public String name;
    public String href;
    public Boolean templated;

    }
    -----------------------------------com.android.Excerpt.java-----------------------------------

    package com.android;


    public class Excerpt {

    public String rendered;
    public Boolean _protected;

    }
    -----------------------------------com.android.Guid.java-----------------------------------

    package com.android;


    public class Guid {

    public String rendered;

    }
    -----------------------------------com.android.Links.java-----------------------------------

    package com.android;

    import java.util.List;

    public class Links {

    public List<Self> self = null;
    public List<Collection> collection = null;
    public List<About> about = null;
    public List<Author> author = null;
    public List<Reply> replies = null;
    public List<WpAttachment> wpAttachment = null;
    public List<WpTerm> wpTerm = null;
    public List<Cury> curies = null;

    }
    -----------------------------------com.android.Outcome.java-----------------------------------

    package com.android;


    public class Outcome {

    public String _6795;
    public String _97587;

    }
    -----------------------------------com.android.Performance.java-----------------------------------

    package com.android;


    public class Performance {

    public String position;
    public String penaltiessaved;
    public String goals;
    public String assists;
    public String yellowcards;
    public String redcards;
    public String owngoals;

    }
    -----------------------------------com.android.Reply.java-----------------------------------

    package com.android;


    public class Reply {

    public Boolean embeddable;
    public String href;

    }
    -----------------------------------com.android.Response.java-----------------------------------

    package com.android;

    import java.util.List;

    public class Response {

    public Integer id;
    public String date;
    public String dateGmt;
    public Guid guid;
    public String modified;
    public String modifiedGmt;
    public String slug;
    public String status;
    public String type;
    public String link;
    public Title title;
    public Content content;
    public Excerpt excerpt;
    public Integer author;
    public Integer featuredMedia;
    public String commentStatus;
    public String pingStatus;
    public String template;
    public List<Integer> leagues = null;
    public List<Integer> seasons = null;
    public List<Integer> venues = null;
    public List<Integer> teams = null;
    public List<String> mainResults = null;
    public Outcome outcome;
    public Integer winner;
    public Integer minutes;
    public List<Integer> players = null;
    public List<Object> offense = null;
    public List<Object> defense = null;
    public List<Integer> staff = null;
    public Results results;
    public List<Performance> performance = null;
    public Links links;

    }
    -----------------------------------com.android.Results.java-----------------------------------

    package com.android;


    public class Results {

    public com.android._0 _0;
    public com.android._6795 _6795;
    public com.android._97587 _97587;

    }
    -----------------------------------com.android.Self.java-----------------------------------

    package com.android;


    public class Self {

    public String href;

    }
    -----------------------------------com.android.Title.java-----------------------------------

    package com.android;


    public class Title {

    public String rendered;

    }
    -----------------------------------com.android.WpAttachment.java-----------------------------------

    package com.android;


    public class WpAttachment {

    public String href;

    }
    -----------------------------------com.android.WpTerm.java-----------------------------------

    package com.android;


    public class WpTerm {

    public String taxonomy;
    public Boolean embeddable;
    public String href;

    }
    -----------------------------------com.android._0.java-----------------------------------

    package com.android;


    public class _0 {

    public String goals;
    public String outcome;

    }
    -----------------------------------com.android._6795.java-----------------------------------

    package com.android;

    import java.util.List;

    public class _6795 {

    public String goals;
    public List<String> outcome = null;

    }
    -----------------------------------com.android._97587.java-----------------------------------

    package com.android;

    import java.util.List;

    public class _97587 {

    public String goals;
    public List<String> outcome = null;

    }

parse model using Gson
Response response = new Gson().fromJson("your_json_string", Response.class);

答案 1 :(得分:0)

你可以http://www.jsonschema2pojo.org/。它将帮助您非常轻松地将数据从JSON解析为对象。

enter image description here

您可以粘贴完整的json数据,它会为您显示对象。

非常容易!

我希望它是帮助你的小工具!