2016-05-08 15 views
2

Gsonを使用してJSONデータをJavaオブジェクトに変換します。しかし、JSON構造体には、フラット化できる余分なフィールドがあります。これはGsonと可能ですか?Gsonを使用してJSON階層を平坦化することはできますか?

(これは説明するのはかなり困難であるため)手の込んだ、JSONはこのようなものになります。

{ 
    "foo": "bar", 
    "data": { 
     "first": 0, 
     "second": 1, 
     "third": 2 
    } 
} 

をこれは、このような二つのクラス、親用とdataのための1つを、生成します

public class Entry { 
    private String foo; 
    private Data data; 
} 

public class Data { 
    private int first; 
    private int second; 
    private int third; 
} 

Javaクラスは次のようになりますように、私は、親オブジェクトにdataフィールドを「フラット化」したいと思います:

これはGsonで可能ですか? TypeAdapters?

+0

は例を与えることをEduardoYáñezPararedaケア@ JsonDeserializer –

+0

を実装するカスタムデシリアライザを書く:あなたはまた、このような何かを行うことができ

public static void main(String[] args) throws IOException, JSONException { String jsonString = "{\n" + " \"foo\": \"bar\",\n" + " \"data\": {\n" + " \"first\": 0,\n" + " \"second\": 1,\n" + " \"third\": 2\n" + " }\n" + "}"; Gson gson = new GsonBuilder() .registerTypeAdapter(Entry.class, new EntryTypeAdapter()).create(); Entry el = gson.fromJson(jsonString, Entry.class); String serialized = gson.toJson(el); System.out.println(serialized); } public static class Entry { public String foo; public Integer first; public Integer second; public Integer third; } 

答えの形式? – manabreak

+0

ここには、JsonDeserializerについての回答が掲載されています。http://stackoverflow.com/questions/6096940/how-do-i-write-a-custom-json-deserializer-for-gson – Yazan

答えて

0

私はあなたにデモを見せてくれるでしょう。実際にこれが欲しいのですか?TypeAdapterコードは読みにくいからです。

private static class EntryTypeAdapter extends TypeAdapter<Entry> { 
    // without registerTypeAdapter(Entry.class, new EntryTypeAdapter()) 
    private Gson gson = new GsonBuilder() 
      // ignore "foo" from deserialization and serialization 
      .setExclusionStrategies(new TestExclStrat()).create(); 

    @Override 
    public void write(JsonWriter out, Entry value) throws IOException { 
     out.beginObject(); 
     out.name("foo"); 
     out.value(value.foo); 
     out.name("data"); 
     out.value(gson.toJson(value)); 
     out.endObject(); 
    } 

    @Override 
    public Entry read(JsonReader in) throws IOException { 
     Entry entry = null; 
     String foo = null; 
     in.beginObject(); 
     while (in.hasNext()) { 
      String name = in.nextName(); 
      if (name.equals("foo")) { 
       foo = in.nextString(); 
      } else if (name.equals("data")) { 
       entry = gson.fromJson(in, Entry.class); 
      } else { 
       in.skipValue(); 
      } 
     } 
     in.endObject(); 
     if(entry!= null) entry.foo = foo; 
     return entry; 
    } 

    public class TestExclStrat implements ExclusionStrategy { 
     public boolean shouldSkipClass(Class<?> arg0) { 
      return false; 
     } 
     public boolean shouldSkipField(FieldAttributes f) { 
      return f.getName().equals("foo"); 
     } 
    } 
} 

はこれでそれをテストすることができます。

// even more complicated version without inner Gson help 
public Entry readOption2(JsonReader in) throws IOException { 
    Entry entry = new Entry(); 
    in.beginObject(); 
    while (in.hasNext()) { 
     String name = in.nextName(); 
     if (name.equals("foo")) { 
      entry.foo = in.nextString(); 
     } else if (name.equals("data")) { 
      in.beginObject(); 
      while (in.hasNext()) { 
       name = in.nextName(); 
       if (name.equals("first")) { 
        entry.first = in.nextInt(); 
       } else if (name.equals("second")) { 
        entry.second = in.nextInt(); 
       } else if (name.equals("third")) { 
        entry.third = in.nextInt(); 
       }else{ 
        in.skipValue(); 
       } 
      } 
      in.endObject(); 
     } else { 
      in.skipValue(); 
     } 
    } 
    in.endObject(); 
    return entry; 
} 
関連する問題