如何删除 JsonObject 的这些"(逗号)"?



这是我的 JsonObject {"Table":"[{

\"FailureIds\":\"\",\"SuccessIds\":\"1167, 8789, 10764, 11935, 937, 938, 939, 940, 3980, 3981, 3982, 3983, 3984, 3985, 3986, 3987, 3988, 3989, 3990, 3991, 6045, 6046, 6047, 6048, 6049, 6050, 6051, 7453, 7454, 7455, 7456, 7457, 8559, 8560, 8561, 8562, 10432, 10433, 10434, 10435, 10436, 10437, 10438, 11705, 11706, 11707, 11708\"}]"}我需要从 [{ 和之后 }] 中删除开头的"和结尾">

我已经尝试过使用JsonElement,如下所示,但没有成功。

String temp="["+successIds.toString()+"]";
JsonObject jsonObjec=new JsonObject();
Gson gson3 = new Gson();
JsonElement jsonElement = gson3.toJsonTree(temp);
jsonObjec.add("Table",jsonElement);

成功ID是一个JSON对象,其中包含失败ID和成功ID,我将其转换为字符串。

请参考下面的代码片段,使用 Google 的 Gson 库创建 JSON,无需额外引号。

Gson gson = new Gson(); //Creates Gson Object
JsonObject jsonObject = new JsonObject();
jsonObject.add("FailureIds", gson.toJsonTree(FailureIds)); //Adding array of failure ids to inner object
jsonObject.add("SuccessIds", gson.toJsonTree(SuccessIds)); //Adding array of success ids to inner object
JsonObject finalObject = new JsonObject();
finalObject.add("Table", gson.toJsonTree(jsonObject));
String jsonStr = gson.toJson(finalObject); //Converts final object to valid Json string 

最终的 JSON 将看起来像

{"Table": {"FailureIds":[1,2,5], "SuccessIds":[4,6,7]}}

最新更新