为了账号安全,请及时绑定邮箱和手机立即绑定

将分号附加到数组中的每个项目,将逗号附加到数组对象集,以形成一个字符串

将分号附加到数组中的每个项目,将逗号附加到数组对象集,以形成一个字符串

子衿沉夜 2023-06-21 16:07:37
我有一个 JSON 数组{"payment":[{"a":"11","b":"21","c":"34","d":"0"},{"a":"54","b":"66","c":"21","d":"76"},{"a":"34","b":"23","c":"43","d":"88"}]} ,我已经迭代数组中的每个项目以形成一个字符串。我试图丢弃数组中“d”等于 0(零)的数组对象。例如:{"a":"11","b":"21","c":"34","d":"0"}.我在每个项目之间添加了一个分号,并在每个数组对象中添加了一个逗号:{54;66;21;76,34;23;43;88}。但我需要这样的最终结果{"payment":"54;66;21;76,34;23;43;88"}:代码:public class TestJson {public static void main(String[] args) {    String jsonArray = "{\"payments\":[{\"a\":\"11\",\"b\":\"21\",\"c\":\"34\",\"d\":\"0\"},{\"a\":\"54\",\"b\":\"66\",\"c\":\"21\",\"d\":\"76\"},{\"a\":\"34\",\"b\":\"23\",\"c\":\"43\",\"d\":\"88\"}]}";    JsonObject jsonObject2 = new Gson().fromJson(jsonArray, JsonObject.class);    String key = "";    JsonObject innerObj = new JsonObject();    StringBuilder joinBuilder = new StringBuilder();    JsonArray paymentsArray = jsonObject2.getAsJsonArray("payments");    for (JsonElement jsonElement : paymentsArray) {        Set<Entry<String, JsonElement>> elemEntry = ((JsonObject) jsonElement).entrySet();        Iterator<Entry<String, JsonElement>> itr = elemEntry.iterator();        String finalVal = "";        String semiColon = "";        while (itr.hasNext()) {            Entry<String, JsonElement> entry = itr.next();            key = entry.getKey();            JsonPrimitive valuePrim = entry.getValue().getAsJsonPrimitive();            if (valuePrim.isString()) {                finalVal = valuePrim.getAsString();            }            joinBuilder.append(semiColon).append(finalVal);            semiColon = ";";        }        joinBuilder.append(",");    }    innerObj.addProperty("payments", joinBuilder.toString());    System.out.println(innerObj.toString());}}输出:{"payments":"11;21;34;0,54;66;21;76,34;23;43;88,"}但我需要删除字符串末尾的逗号,如果“d”字段为 0,则丢弃整个数组对象。前任:{11;21;34;0}
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

我解决了这个问题。


代码:


public static void main(String[] args) {

    String jsonArray = "{\"payments\":[{\"a\":\"11\",\"b\":\"21\",\"c\":\"34\",\"d\":\"0\"},{\"a\":\"54\",\"b\":\"66\",\"c\":\"21\",\"d\":\"76\"},{\"a\":\"34\",\"b\":\"23\",\"c\":\"43\",\"d\":\"88\"}]}";

    JsonObject jsonObject2 = new Gson().fromJson(jsonArray, JsonObject.class);

    JsonObject innerObj = new JsonObject();

    StringBuilder joinBuilder = new StringBuilder();

    Map<String, String> testMap = new LinkedHashMap<String, String>();

    JsonArray paymentsArray = jsonObject2.getAsJsonArray("payments");

    for (JsonElement jsonElement : paymentsArray) {

        Set<Entry<String, JsonElement>> elemEntry = ((JsonObject) jsonElement).entrySet();

        Iterator<Entry<String, JsonElement>> itr = elemEntry.iterator();

        while (itr.hasNext()) {

            Entry<String, JsonElement> entry = itr.next();

            testMap.put(entry.getKey(), entry.getValue().getAsString());

        }

        if (testMap.get("d").equals("0")) {

            testMap.clear();

        } else {

            joinBuilder.append(StringUtils.join(testMap.values(), ';')).append(",");

        }

    }

    joinBuilder.deleteCharAt(joinBuilder.length() - 1);

    innerObj.addProperty("payments", joinBuilder.toString());

    System.out.println(innerObj.toString());

}

输出:{"payments":"54;66;21;76,34;23;43;88"}


查看完整回答
反对 回复 2023-06-21
  • 1 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信