如何在Java中返回多个JSON项目的列表
我试图弄清楚如何返回多个JSON项.现在,我能够返回单个JSON,如下所示:
I am trying to figure out how to return multiple JSON items. Right now I am able, to return a single JSON like so:
{
"result": {
"userId": "abcde123",
"telephoneNumber": "1-555-5555555"
},
"error": null
}
但是我想返回多个JSON项目,像这样:
But I would like to return multiple JSON items, like so:
{
"result": {{
"userId": "abcde123",
"telephoneNumber": "1-555-5555555"
}
{
"userId": "fghi456",
"telephoneNumber": "1-333-3333333"
}
},
"error": null
}
我可以将多个JSON项目作为字符串查看,如下所示,但是我想将其作为多个JSON项目返回,但是我不知道如何做:
I can view the multiple JSON items as string, like below, but I would like to return it as multiple JSON items, but I don't know how:
[LDAPModel(userId=abcde123, telephoneNumber=1-555-5555555), LDAPModel(userId=fghi456, telephoneNumber=1-333-3333333]
我是Java的完全入门者,并且对Java的语法不甚了解.但是我从SpringBoot得到了这些代码(包括下面的代码).我真的不明白它在做什么,所以我不知道如何创建列表的输出.
I am a complete beginner in Java, and I don't know the syntax or much in Java. But I was given these codes (including the one below) from SpringBoot; I really don't understand what it is doing, and so I have no idea how create an output of list.
目前,这就是我得到的:
Currently, this is what I was given:
public Optional<LDAPModel> getDirectReports(String cdsID) {
LdapQuery ldapQuery = LdapQueryBuilder.query()
.searchScope(SearchScope.SUBTREE)
.where("objectclass").is("person")
.and("managerID").like(cdsID);
List<LDAPModel> ldapModelList = ldapTemplate.search(ldapQuery, (Attributes attrs) ->
LDAPModel.builder()
.userId(getValue(attrs, "userid"))
.telephoneNumber(getValue(attrs, "phoneNumber"))
.build());
// for (int ii = 0; ii < ldapModelList.size(); ii++) {
// Optional.of(ldapModelList.get(ii));
// ldapModelList.isEmpty() ? Optional.empty() : Optional.of(ldapModelList.get(ii));
// }
return ldapModelList.isEmpty() ? Optional.empty() : Optional.of(ldapModelList.get(0));
}
我尝试将其放入循环中(如上面注释掉的代码中所示),但是我不知道如何创建列表.我尝试删除get(0),但是出现语法错误...我尝试了很多事情,但这没有帮助.
I tried putting it in a loop (like in the commented out code above), but I don't know how create a list. I tried removing the get(0), but there was a syntax error... There are many things I tried, but it just did not help.
有人可以帮忙吗?
更新/谢谢大家的回答.我发布了一个后续问题此处.如果有机会,请帮帮我.谢谢.
Update/ Thank you all for your answers. I posted a follow up question here. If you have a chance, please help me out. Thanks.
首先,我想指出您的JSON格式不正确.当您想用JSON表示多个对象时,应使用方括号并用逗号分隔每个对象:
First of all I would like to point out that your JSON isn't formatted properly. When you want to represent multiple objects in JSON you should use square brackets and separate each object with a comma:
{
"result": [
{
"userId": "abcde123",
"telephoneNumber": "1-555-5555555"
},
{
"userId": "fghi456",
"telephoneNumber": "1-333-3333333"
}
],
"error": null
}
尝试删除get(0)时Java代码不起作用的原因是因为方法 public Optional< LDAPModel>getDirectReports(String cdsID)
返回 Optional< LDAPModel>
类型,并且通过删除get(0),您正在有效地尝试返回 Optional< List< List< LDAPModel>>> 代码>.如果您希望该方法返回列表而不是单个对象,则可以将返回类型更改为
Optional< List< LDAPModel>>
,然后安全地删除get(0).
The reason your Java code does not work when you try and remove get(0) is because the method public Optional<LDAPModel> getDirectReports(String cdsID)
returns an Optional<LDAPModel>
type and by removing get(0) your are effectively trying to return an Optional<List<LDAPModel>>
. If you want the method to return a list instead of a single object you can change the return type to Optional<List<LDAPModel>>
and then safely remove get(0).
public Optional<List<LDAPModel>> getDirectReports(String cdsID) {
LdapQuery ldapQuery = LdapQueryBuilder.query()
.searchScope(SearchScope.SUBTREE)
.where("objectclass").is("person")
.and("managerID").like(cdsID);
List<LDAPModel> ldapModelList = ldapTemplate.search(ldapQuery, (Attributes attrs) ->
LDAPModel.builder()
.userId(getValue(attrs, "userid"))
.telephoneNumber(getValue(attrs, "phoneNumber"))
.build());
return ldapModelList.isEmpty() ? Optional.empty() : Optional.of(ldapModelList);
}