2017-02-10 15 views
1

私が持っているこのようなJSON文字列:Gson-解析ArrayListにJSONオブジェクトのJSON配列<org.json.JSONObject>

私はこのPOJOモデルに解析する
{ 
    "r": [ 
    { 
     "pic": "1.jpg", 
     "name": "Name1" 
    }, 
    { 
     "pic": "2.jpg", 
     "name": "Name2" 
    }, 
    { 
     "pic": "3.jpg", 
     "name": "Name3" 
    } 
    ] 
} 

public class Catalog { 
    @SerializedName("r") 
    @Expose 
    private List<JSONObject> r = new ArrayList<JSONObject>(); 

    public List<JSONObject> getR() { 
     return r; 
    } 

    public void setR(List<JSONObject> r) { 
     this.r = r; 
    } 
} 
私はこの方法を解析しています

Catalog cat = new Gson().fromJson(jsonString,Catalog.class); 

しかし、最終的には、このJSONに

を取得しています
{ 
    "r": [ 
    { 
     "nameValuePairs": {} 
    }, 
    { 
     "nameValuePairs": {} 
    }, 
    { 
     "nameValuePairs": {} 
    } 
    ] 
} 

com.google.gson.JsonObjectを使用しないことにご注意ください。 私は使いたいorg.json.JSONObjectです。私のコードのほとんどがそれを使用しているので、これをどのように達成するのですか?

+0

あなた 'JsonDeseriailzer 'を実装し、 '.registerTypeAdapter(JSONObject.class、jsonObjectJsonDeserializerInstance)'と 'GsonBuilder'でそれを登録します。しかし、 'pic'と' name'フィールドを持つJavaマッピングではなく、 'JSONObject'を使う必要があるのはなぜですか? –

+0

なぜ 'org.json.JSONObject'が必要なのですか? –

+0

'JSONObjects'の' JSONArray'を 'JSONObjects'の' List'に変換する理由は何ですか? 'JSONArray'と' List'で 'JSONObjects'を繰り返し処理することができます。 –

答えて

0

使用して、それがすでに他の回答やコメントで述べたように、あなたはおそらく、本当にいくつかのためにorg.json.JSONObjectを使用したくない場合があります理由。しかしが必要な場合は、org.json.JSONObject -aware Gsonインスタンスを作成するだけです。

final class JSONObjectJsonDeserializer 
     implements JsonDeserializer<JSONObject> { 

    // The implementation is fully thread-safe and can be instantiated once 
    private static final JsonDeserializer<JSONObject> jsonObjectJsonDeserializer = new JSONObjectJsonDeserializer(); 

    // Type tokens are immutable values and therefore can be considered constants (and final) and thread-safe as well 
    private static final TypeToken<Map<String, Object>> mapStringToObjectTypeToken = new TypeToken<Map<String, Object>>() { 
    }; 

    private JSONObjectJsonDeserializer() { 
    } 

    static JsonDeserializer<JSONObject> getJsonObjectJsonDeserializer() { 
     return jsonObjectJsonDeserializer; 
    } 

    @Override 
    public JSONObject deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context) { 
     // Convert the input jsonElement as if it were a Map<String, Object> (a generic representation for JSON objectS) 
     final Map<String, Object> map = context.deserialize(jsonElement, mapStringToObjectTypeToken.getType()); 
     // And forward the map to the JSONObject constructor - it seems to accept it nice 
     return new JSONObject(map); 
    } 

} 

Gsonスレッドセーフに設計されており、直列化または直列化復元が必要になるたびにインスタンス化する必要はありません。

private static final Gson gson = new GsonBuilder() 
     .registerTypeAdapter(JSONObject.class, getJsonObjectJsonDeserializer()) 
     .create(); 

そして最後に:

final Catalog catalog = gson.fromJson(jsonString, Catalog.class); 
out.println(catalog.getR()); 

次の結果:

[{ "名前": "NAME1"、 "PIC": "1.JPG"}、{ "名前": "Name2は"、 "PIC": "2.JPG"} 、{"name": "Name3"、 "pic": "3.jpg"}]

とにかく、私はあなたのマッピングモデルを再設計することをお勧めします。

+1

ありがとうございました。それは期待どおりに動作します。しかし、私はJSONObjectを置き換えるpojoを作成しました。 Deserializerで解析する際のオーバーヘッドではなく、今すぐマップする方がよいでしょう。 – Raj

0

JSONObjectは必要ありません。

この

// is wrapped class for serialized json. 
public class JsonExample 
{ 
    List<Catalog> r; 
} 

public class Catalog { 
    private String pic; 
    private String name; 

    public String getPic() { 
     return pic; 
    } 

    public String getName() { 
     return name; 
    } 
} 

JsonExample example = new Gson().fromJson(json, JsonExample.class); 

追加してください - JSONObject

JSONObject obj = new JSONObject(json); 
JSONArray arr = obj.getJSONArray("r"); 

List<Catalog> cataList = new ArrayList<>(); 

for(int i = 0 ; i < arr.length() ; ++i) 
{ 
    cataList.add(new Catalog(arr.getJSONObject(i))); 
} 

public class Catalog { 
    private String pic; 
    private String name; 

    public Catalog(JSONObject obj) throws JSONException 
    { 
     pic = obj.getString("pic"); 
     name = obj.getString("name"); 
    } 

    public String getPic() { 
     return pic; 
    } 

    public String getName() { 
     return name; 
    } 
} 
0

あなたの場合、gsonライブラリの使用は全く必要ないと思います。 org.jsonのみが問題全体を解決できます。

例えば:

JSONObject json = new JSONObject(jsonString); 

JSONArray jsonArray = json.getJSONArray("r"); 

List<JSONObject> jsonList = new ArrayList<>(); 
for (int i = 0; i < jsonArray.length(); i++) { 
    jsonList.add(jsonArray.getJSONObject(i)); 
} 

Catalog catalog = new Catalog();   
catalog.setR(jsonList);