[英]Add a Key value to Json String generated from GSON.toJSON
String jsonResponse=Utils.getGsonInstance().toJson(Object);
jsonResponse returns : jsonResponse 返回:
[
{
"Key":"1",
"Code": "11",
},
{
"key":"2",
"code": "22",
}
]
End result I am looking for is to wrap this JSON-String in another Key Eg我正在寻找的最终结果是将此 JSON-String 包装在另一个 Key Eg 中
{
"MainObj":
[
{
"Key":"1",
"Code": "11",
},
{
"key":"2",
"code": "22",
}
]
}
Is there a way I can achieve this using GSON Api ?有没有办法使用 GSON Api 实现这一点?
I tried ::我试过 ::
JSONObject jsonObject = new JSONObject();
jsonObject.put("MainObj",jsonResponse);
Output I am getting is :我得到的输出是:
{"MainObj": "[{\"Key\":\"1",\"Code\":\"11\"}, {\"Key\":\"2",\"Code\":\"22\"}]"}
Continue with GSON like :继续使用 GSON,例如:
public class MainObj {
@SerializedName("MainObj")
public List<Key> Main;
public class Key {
@SerializedName("Key")
public String Key;
@SerializedName("code")
public String Code;
}
}
And change并改变
JSONObject jsonObject = new JSONObject();
jsonObject.put("MainObj",jsonResponse);
by经过
String tmp = new Gson().toJson(new MainObj());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.