2016-03-24 16 views
0
String jsonResponse=Utils.getGsonInstance().toJson(Object); 

jsonResponse zurück:einen Schlüsselwert In den JSON-String von GSON.toJSON erzeugt

[ 
    { 
    "Key":"1", 
    "Code": "11", 
    }, 
    {  
    "key":"2", 
    "code": "22", 
    } 
] 

Endergebnis ich suche ist dieses JSON-String in einem anderen Schlüssel Z. B. wickeln

{ 
"MainObj": 
    [ 
    { 
    "Key":"1", 
    "Code": "11", 
    }, 
    {  
    "key":"2", 
    "code": "22", 
    } 
] 
} 

Gibt es eine Möglichkeit, die ich mit GSON Api erreichen kann?

Ich versuchte ::

JSONObject jsonObject = new JSONObject(); 
    jsonObject.put("MainObj",jsonResponse); 

Output Ich erhalte ist:

{"MainObj": "[{\"Key\":\"1",\"Code\":\"11\"}, {\"Key\":\"2",\"Code\":\"22\"}]"} 
+0

'JSONObject JsonObject = new JSONObject(); jsonObject.put ("MainObj", jsonResponse.get (i)); ' –

+0

Ich weiß nicht, ob es Ihre Anforderungen erfüllen. Aber so machen wir das bei Android, also habe ich das als Kommentar hinzugefügt. –

Antwort

0

mit Gson weiterfahren:

public class MainObj { 
    @SerializedName("MainObj") 
    public List<Key> Main; 

    public class Key { 
     @SerializedName("Key") 
     public String Key; 

     @SerializedName("code") 
     public String Code; 
    } 
} 

Und ändern

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("MainObj",jsonResponse); 

von

String tmp = new Gson().toJson(new MainObj()); 
Verwandte Themen