2017-11-08 37 views
0

このJSONでは、自動化されたJSONからPOJOへの変換に失敗します。JSONレスポンスをPOJOに変換するにはどうすればいいですか?

アイテムの数はリクエストごとに異なりますのでご注意ください。 ここではJSONレスポンスを2つの項目で含めています。

{ 
    "status": 1, 
    "complete": 1, 
    "list": { 
     "734233858": { 
      "item_id": "734233858", 
      "resolved_id": "734233858", 
      "given_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/", 
      "given_title": "", 
      "favorite": "0", 
      "status": "0", 
      "time_added": "1466459879", 
      "time_updated": "1466459862", 
      "time_read": "0", 
      "time_favorited": "0", 
      "sort_id": 1, 
      "resolved_title": "Developing Single Page Web Applications using Java 8, Spark, MongoDB, and AngularJS", 
      "resolved_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/", 
      "excerpt": "In this post you will learn how to use a micro framework called Spark to build a RESTful backend. The RESTful backend is consumed by a single page web application using AngularJS and MongoDB for data storage. I’ll also show you how to run Java 8 on OpenShift.", 
      "is_article": "1", 
      "is_index": "0", 
      "has_video": "0", 
      "has_image": "1", 
      "word_count": "2727" 
     }, 
     "1015284226": { 
      "item_id": "1015284226", 
      "resolved_id": "1015284226", 
      "given_url": "https://sparktutorials.github.io/2015/08/04/spark-video-tutorials.html", 
      "given_title": "", 
      "favorite": "0", 
      "status": "0", 
      "time_added": "1466458750", 
      "time_updated": "1466458737", 
      "time_read": "0", 
      "time_favorited": "0", 
      "sort_id": 0, 
      "resolved_title": "Spark Video Tutorials", 
      "resolved_url": "http://sparktutorials.github.io/2015/08/04/spark-video-tutorials.html", 
      "excerpt": "Our friends over at learnhowtoprogram.com have been working on a series of Java courses for beginners, all of which feature Spark. This post contains an overview of these courses with direct links to their videos.", 
      "is_article": "1", 
      "is_index": "0", 
      "has_video": "0", 
      "has_image": "0", 
      "word_count": "41" 
     } 
    }, 
    "error": null, 
    "search_meta": { 
     "search_type": "normal" 
    }, 
    "since": 1509309762 
} 

このJSONオブジェクトのPOJOはどのようになりますか?

答えて

0

ためのPOJOである<String, InnerObject>、と<String, Object>マップを置き換えることができ、JSONオブジェクト内のlistプロパティは、実際には、内側のではなく、要素の配列でなければなりませんJSONオブジェクト。ただし、データをモデリングするために以下を使用することができます。

import org.codehaus.jackson.annotate.JsonProperty; 

import java.util.Map; 

class POJO { 

    private Integer status; 
    private Integer complete; 
    private String error; 
    private Long since; 

    @JsonProperty("search_meta") 
    private SearchMeta searchMeta; 

    private Map<String, Item> list; 

    public Integer getStatus() { 
     return status; 
    } 

    public void setStatus(Integer status) { 
     this.status = status; 
    } 

    public Integer getComplete() { 
     return complete; 
    } 

    public void setComplete(Integer complete) { 
     this.complete = complete; 
    } 

    public String getError() { 
     return error; 
    } 

    public void setError(String error) { 
     this.error = error; 
    } 

    public Long getSince() { 
     return since; 
    } 

    public void setSince(Long since) { 
     this.since = since; 
    } 

    public SearchMeta getSearchMeta() { 
     return searchMeta; 
    } 

    public void setSearchMeta(SearchMeta searchMeta) { 
     this.searchMeta = searchMeta; 
    } 

    public Map<String, Item> getList() { 
     return list; 
    } 

    public void setList(Map<String, Item> list) { 
     this.list = list; 
    } 
} 

class SearchMeta { 

    @JsonProperty("search_type") 
    private String searchType; 

    public String getSearchType() { 
     return searchType; 
    } 

    public void setSearchType(String searchType) { 
     this.searchType = searchType; 
    } 
} 

class Item { 

    @JsonProperty("item_id") 
    private String itemId; 

    @JsonProperty("resolved_id") 
    private String resolvedId; 

    // more attributes here 

    public String getItemId() { 
     return itemId; 
    } 

    public void setItemId(String itemId) { 
     this.itemId = itemId; 
    } 

    public String getResolvedId() { 
     return resolvedId; 
    } 

    public void setResolvedId(String resolvedId) { 
     this.resolvedId = resolvedId; 
    } 
} 

Hereは、JSONの解析の詳細です。

1

番号がランダムなので、listオブジェクトを合理的に解析することはできません。地図を作成する必要があります。それ以外の場合、残りのデータはGsonによって解析可能です。

class Foo { 
    int status; 
    int complete; 
    TreeMap<String, Object> list; 
    Object error; 
    SearchMeta search_meta; 
    long since; 
} 

class SearchMeta { 
    String search_type; 
} 

あなたはInnerObjectこのオブジェクト理想的

{ 
    "item_id": "734233858", 
    "resolved_id": "734233858", 
    "given_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/", 
    "given_title": "", 
    "favorite": "0", 
    ... 
+0

ありがとうございましたcricket_007、私は15の評判になると私の投票が表示されます – Tlink

関連する問題