![](/img/trans.png)
[英]Serializing nested org.json.JSONObject using gson.toJson adding into “map” key
[英]Add a Key value to Json String generated from GSON.toJSON
String jsonResponse=Utils.getGsonInstance().toJson(Object);
jsonResponse 返回:
[
{
"Key":"1",
"Code": "11",
},
{
"key":"2",
"code": "22",
}
]
我正在尋找的最終結果是將此 JSON-String 包裝在另一個 Key Eg 中
{
"MainObj":
[
{
"Key":"1",
"Code": "11",
},
{
"key":"2",
"code": "22",
}
]
}
有沒有辦法使用 GSON Api 實現這一點?
我試過 ::
JSONObject jsonObject = new JSONObject();
jsonObject.put("MainObj",jsonResponse);
我得到的輸出是:
{"MainObj": "[{\"Key\":\"1",\"Code\":\"11\"}, {\"Key\":\"2",\"Code\":\"22\"}]"}
繼續使用 GSON,例如:
public class MainObj {
@SerializedName("MainObj")
public List<Key> Main;
public class Key {
@SerializedName("Key")
public String Key;
@SerializedName("code")
public String Code;
}
}
並改變
JSONObject jsonObject = new JSONObject();
jsonObject.put("MainObj",jsonResponse);
經過
String tmp = new Gson().toJson(new MainObj());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.