我想通过Domino Access Services(DAS)访问Domino数据作为Java中的REST提供程序,例如
String url = "http://malin1/fakenames.nsf/api/data/collections/name/groups";
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createParser(new URL(url));
JsonNode rootNode = mapper.readTree(parser);
但是,我注意到DAS将JSON绑定在方括号中:
[
{
"@entryid":"1-D68BB54DEA77AC8085256B700078923E",
"@unid":"D68BB54DEA77AC8085256B700078923E",
"@noteid":"1182",
"@position":"1",
"@read":true,
"@siblings":3,
"@form":"Group",
"name":"LocalDomainAdmins",
"description":"This group should contain all Domino administrators in your domain. Most system databases and templates give people in this group Manager access."
},
{
"@entryid":"3-9E6EABBF405A1A9985256B020060E64E",
"@unid":"9E6EABBF405A1A9985256B020060E64E",
"@noteid":"F46",
"@position":"3",
"@read":true,
"@siblings":3,
"@form":"Group",
"name":"OtherDomainServers",
"description":"You should add all Domino servers in other domains with which you commonly replicate to this group."
}
]
如何轻松摆脱这些括号?
答案 0 :(得分:0)
如前所述,你应该保持原样。例如,您可以使用Jackson解析.JSON数组。
找到下面的示例代码段
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
...
String response = ... your posted string
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode taskIdsjsonNode = mapper.readTree(response);
for (JsonNode next : taskIdsjsonNode) {
System.out.printf("%s: %s%n", "@entryid", next.get("@entryid"));
System.out.printf("%s: %s%n", "name", next.get("name"));
}
} catch (.... ) {
// your exception handling goes here
}
<强>输出强>
@entryid: "1-D68BB54DEA77AC8085256B700078923E"
name: "LocalDomainAdmins"
@entryid: "3-9E6EABBF405A1A9985256B020060E64E"
name: "OtherDomainServers"
答案 1 :(得分:0)
括号不是令人讨厌的,而是正确的表示法。要访问竞争,只需在客户端脚本中使用[0]或使用您喜欢的Java中的JSON解析器。
答案 2 :(得分:0)
也许这里的解释可以提供帮助:
基本上你通过Jersey客户端建立对DAS的调用,然后通过Jackson库将json解析为java中的地图。
在解析过程中,您可以定义要解析和转换它们的值。
看一下Person类...