2017-08-05 6 views
0

を作るために、私は実際に私は、JSONは(期待JSON)下記の形式になりたい私に次の応答Gsonカスタムデシリアライズ異なるJSON出力

{ 
    "took": 3, 
    "timed_out": false, 
    "_shards": { 
    "total": 3, 
    "successful": 3, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 3, 
    "max_score": 123, 
    "hits": [ 
     { 
     "_index": "123", 
     "_type": "bar", 
     "_id": "123", 
     "_score": 323, 
     "_source": { 
      "employeeNo": 239, 
      "employeeName": "John", 
      "employeeRank": 21, 
      "employeeNationality": "Indian", 
      "employeeNickName": "KingKong" 
     } 
     }, 
     { 
     "_index": "234", 
     "_type": "bar", 
     "_id": "124", 
     "_score": 324, 
     "_source": { 
      "employeeNo": 241, 
      "employeeName": "Sunny", 
      "employeeRank": 19, 
      "employeeNickName": "Jakk" 
     } 
     }, 
     { 
     "_index": "235", 
     "_type": "bar", 
     "_id": "125", 
     "_score": 325, 
     "_source": { 
      "employeeNo": 251, 
      "employeeName": "Pollard", 
      "employeeRank": 10 
     } 
     } 
    ] 
    }, 
    "aggregations": { 
    "foo": { 
     "buckets": [ 
     { 
      "key": 123, 
      "doc_count": 123 
     } 
     ] 
    } 
    } 
} 

を与えるREST APIサービスを持っています

これを達成するための
[ 
    { 
    "employeeNo": 239, 
    "employeeName": "John", 
    "employeeRank": 21, 
    "employeeNationality": "Indian", 
    "employeeNickName": "KingKong" 
    }, 
    { 
    "employeeNo": 241, 
    "employeeName": "Sunny", 
    "employeeRank": 19, 
    "employeeNickName": "Jakk" 
    }, 
    { 
    "employeeNo": 251, 
    "employeeName": "Pollard", 
    "employeeRank": 10 
    } 
] 

は、私は次のコードを書いている

List<Map<String, String>> myResults = new ArrayList<Map<String, String>>(); 
Gson serializer = new GsonBuilder().create(); 
String responseDetails = doHttpPost(urlToQuery, serializer.toJson(requestBody)); 
Map<String,Object> restResponse = (Map<String,Object>)serializer.fromJson(responseDetails,Object.class); 
if(restResponse.containsKey("hits")){ 
    Map<String, Object> hits = (Map<String, Object>) restResponse.get("hits"); 
    if(hits.containsKey("hits")) { 
     List<Map<String, Object>> hitDetails = (List<Map<String, Object>>) hits.get("hits"); 
     for (Map<String, Object> hitDetail : hitDetails) { 
      Map<String, Object> sources = (Map<String, Object>) hitDetail.get("_source"); 
      myResults.add(sources); 
     } 
    } 
} 

上記のコードでは、私はresponseDetails変数に入れた文字列としてREST APIからの応答を取得します。また、私はそれを反復処理し、私の期待JSON応答を形成

Map<String,Object> restResponse = (Map<String,Object>)serializer.fromJson(responseDetails,Object.class); 

さらにGson fromJson機能を使用してMap<String,Object>にそれをデシリアライズ。

私の期待するところは:このアプローチでは、期待されるjsonを得るために、逆直列化されたjsonを繰り返し処理し、手動で作成する必要があります。

fromJsonの戻り値として、デシリアライズ処理中に応答jsonを取得する方法はありますか。すなわち

List<Map<String, String>> myResults = serializer.fromJson(responseDetails); 

以下のように何か私たちはfromJson機能を無効にしてList<Map<String, String>>を返す私たち自身のカスタムデシリアライザを書くことができます。

パフォーマンスを向上させるための提案やその他の方法が必要です。この後

RESTサービスのために、より明確化

答えて

1

を必要に応じて、私は通常、私はあなたが(Githubで利用可能)例JSONUtilsのために、Javaのモデル変換プログラム/サービスにいくつかのJSONを使用することを示唆して知らせてください

ServiceResultクラス

import com.fasterxml.jackson.annotation.JsonProperty; 

public class ServiceResult { 
    @JsonProperty("took") 
    public int getTook() { 
     return this.took; 
    } 

    public void setTook(int took) { 
     this.took = took; 
    } 

    int took; 

    @JsonProperty("timed_out") 
    public boolean getTimed_out() { 
     return this.timed_out; 
    } 

    public void setTimed_out(boolean timed_out) { 
     this.timed_out = timed_out; 
    } 

    boolean timed_out; 

    @JsonProperty("_shards") 
    public Shards get_shards() { 
     return this._shards; 
    } 

    public void set_shards(Shards _shards) { 
     this._shards = _shards; 
    } 

    Shards _shards; 

    @JsonProperty("hits") 
    public Hits getHits() { 
     return this.hits; 
    } 

    public void setHits(Hits hits) { 
     this.hits = hits; 
    } 

    Hits hits; 

    @JsonProperty("aggregations") 
    public Aggregations getAggregations() { 
     return this.aggregations; 
    } 

    public void setAggregations(Aggregations aggregations) { 
     this.aggregations = aggregations; 
    } 

    Aggregations aggregations; 

} 
:変換は、Javaモデルを持っています

シャードクラス

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Shards { 
    @JsonProperty("total") 
    public int getTotal() { 
     return this.total; 
    } 

    public void setTotal(int total) { 
     this.total = total; 
    } 

    int total; 

    @JsonProperty("successful") 
    public int getSuccessful() { 
     return this.successful; 
    } 

    public void setSuccessful(int successful) { 
     this.successful = successful; 
    } 

    int successful; 

    @JsonProperty("failed") 
    public int getFailed() { 
     return this.failed; 
    } 

    public void setFailed(int failed) { 
     this.failed = failed; 
    } 

    int failed; 

} 

Sourceクラス

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Source { 
    @JsonProperty("employeeNo") 
    public int getEmployeeNo() { 
     return this.employeeNo; 
    } 

    public void setEmployeeNo(int employeeNo) { 
     this.employeeNo = employeeNo; 
    } 

    int employeeNo; 

    @JsonProperty("employeeName") 
    public String getEmployeeName() { 
     return this.employeeName; 
    } 

    public void setEmployeeName(String employeeName) { 
     this.employeeName = employeeName; 
    } 

    String employeeName; 

    @JsonProperty("employeeRank") 
    public int getEmployeeRank() { 
     return this.employeeRank; 
    } 

    public void setEmployeeRank(int employeeRank) { 
     this.employeeRank = employeeRank; 
    } 

    int employeeRank; 

    @JsonProperty("employeeNationality") 
    public String getEmployeeNationality() { 
     return this.employeeNationality; 
    } 

    public void setEmployeeNationality(String employeeNationality) { 
     this.employeeNationality = employeeNationality; 
    } 

    String employeeNationality; 

    @JsonProperty("employeeNickName") 
    public String getEmployeeNickName() { 
     return this.employeeNickName; 
    } 

    public void setEmployeeNickName(String employeeNickName) { 
     this.employeeNickName = employeeNickName; 
    } 

    String employeeNickName; 

} 

ヒットクラス

import java.util.List; 

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Hits { 
    @JsonProperty("total") 
    public int getTotal() { 
     return this.total; 
    } 

    public void setTotal(int total) { 
     this.total = total; 
    } 

    int total; 

    @JsonProperty("max_score") 
    public int getMax_score() { 
     return this.max_score; 
    } 

    public void setMax_score(int max_score) { 
     this.max_score = max_score; 
    } 

    int max_score; 

    @JsonProperty("hits") 
    public List<Hit> getHits() { 
     return this.hits; 
    } 

    public void setHits(List<Hit> hits) { 
     this.hits = hits; 
    } 

    List<Hit> hits; 

} 

ヒットクラス

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Hit { 
    @JsonProperty("_index") 
    public String get_index() { 
     return this._index; 
    } 

    public void set_index(String _index) { 
     this._index = _index; 
    } 

    String _index; 

    @JsonProperty("_type") 
    public String get_type() { 
     return this._type; 
    } 

    public void set_type(String _type) { 
     this._type = _type; 
    } 

    String _type; 

    @JsonProperty("_id") 
    public String get_id() { 
     return this._id; 
    } 

    public void set_id(String _id) { 
     this._id = _id; 
    } 

    String _id; 

    @JsonProperty("_score") 
    public int get_score() { 
     return this._score; 
    } 

    public void set_score(int _score) { 
     this._score = _score; 
    } 

    int _score; 

    @JsonProperty("_source") 
    public Source get_source() { 
     return this._source; 
    } 

    public void set_source(Source _source) { 
     this._source = _source; 
    } 

    Source _source; 

} 

のFooクラス

import java.util.List; 

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Foo { 
    @JsonProperty("buckets") 
    public List<Bucket> getBuckets() { 
     return this.buckets; 
    } 

    public void setBuckets(List<Bucket> buckets) { 
     this.buckets = buckets; 
    } 

    List<Bucket> buckets; 

} 

バケットクラス

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Bucket { 
    @JsonProperty("key") 
    public int getKey() { 
     return this.key; 
    } 

    public void setKey(int key) { 
     this.key = key; 
    } 

    int key; 

    @JsonProperty("doc_count") 
    public int getDoc_count() { 
     return this.doc_count; 
    } 

    public void setDoc_count(int doc_count) { 
     this.doc_count = doc_count; 
    } 

    int doc_count; 

} 

集計クラス

import com.fasterxml.jackson.annotation.JsonProperty; 

public class Aggregations { 
    @JsonProperty("foo") 
    public Foo getFoo() { 
     return this.foo; 
    } 

    public void setFoo(Foo foo) { 
     this.foo = foo; 
    } 

    Foo foo; 

} 

ServiceResultモデルがあり、実際にSourcモデルが必要です。

import java.io.File; 
import java.util.List; 
import java.util.stream.Collectors; 

import org.apache.commons.io.IOUtils; 

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 

public class Test { 
    public static void main(String[] args) { 
     try { 

      // String responseDetails = doHttpPost(urlToQuery, serializer.toJson(requestBody)); 
      // using your json from file 
      String responseDetails = new String(IOUtils.toByteArray(new File("test.json").toURI()), "UTF-8"); 

      Gson serializer = new GsonBuilder().create(); 
      ServiceResult example = serializer.fromJson(responseDetails, ServiceResult.class); 

      // your expected list 
      List<Source> sourceList = example.getHits() 
        .getHits() 
        .stream().map(h -> h.get_source()) 
        .collect(Collectors.toList()); 

      // you can just serialize it to json or do whatever you want 
      String json = serializer.toJson(sourceList); 
      System.out.println(json); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

のようなJavaコレクションストリームを使用して抽出することができます。まだ 'map'を使用して繰り返しています。 –

関連する問題