detailsArray = new ArrayList>(); JSONOb-6ren">
gpt4 book ai didi

java - json 格式的 hashmap 输出数组

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:53 25 4
gpt4 key购买 nike

我编写了以下代码,它给出了以下输出:

String jsonInString = "NORESPONSE";
List<Map<String , String>> detailsArray = new ArrayList<Map<String,String>>();
JSONObject json = new JSONObject();
Map<String, String> details = new HashMap<String, String>();

for (int i = 0; i < split.length; i++) {
String entry = split[i];
j++;

String entrySplit[] = entry.split("\\:");
details.put("PrimaryEmail", entrySplit[0]);
details.put("DisplayName", entrySplit[1]);

allEntries.put("Entry " + j , details);
//detailsArray.add(i, details);
}

json.putAll(allEntries);
jsonInString = json.toJSONString();
logger.info(jsonInString);

输出:

{
"Entry 2": {
"DisplayName": "OICT-Focal Points All",
"PrimaryEmail": "oict-oict-globalvtc@un87.org"
},
"Entry 3": {
"DisplayName": "OICT-Focal Points All",
"PrimaryEmail": "oict-oict-globalvtc@un87.org"
},
"Entry 1": {
"DisplayName": "OICT-Focal Points All",
"PrimaryEmail": "oict-oict-globalvtc@un87.org"
}
}

但是,我想要以下类型的输出。我的代码需要进行哪些更改?如果有人能帮助我,那就太好了。提前致谢

[{"DisplayName":"OICT-Focal Points All","PrimaryEmail":"oict-oict-globalvtc@un87.org"},{"DisplayName":"OICT-Focal Points All","PrimaryEmail":"oict-oict-globalvtc@un87.org"},{"DisplayName":"OICT-Focal Points All","PrimaryEmail":"oict-oict-globalvtc@un87.org"}]

最佳答案

根据@Pete的评论,这里是示例代码(未经测试):

String jsonInString = "NORESPONSE";
JSONArray json = new JSONArray();
JSONObject jsonObj;

for (int i = 0; i < split.length; i++) {
String entry = split[i];
jsonObj = new JSONObject();

String entrySplit[] = entry.split("\\:");
jsonObj.put("PrimaryEmail", entrySplit[0]);
jsonObj.put("DisplayName", entrySplit[1]);

json.put(jsonObj);
}

jsonInString = json.toJSONString();
logger.info(jsonInString);

关于java - json 格式的 hashmap 输出数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44029901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com