简体   繁体   中英

remove square brackets from domino access services

I want to access Domino data via the Domino Access Services (DAS) as REST provider in java eg

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);

however, I notice DAS binds the JSON in square brackets:

[
  {
      "@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."
  }
]

How can I easily get rid of these brackets?

As already mentioned you should leave them intact. You can parse theJSON array for example with Jackson.

find an example snippet below

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
}

output

@entryid: "1-D68BB54DEA77AC8085256B700078923E"
name: "LocalDomainAdmins"
@entryid: "3-9E6EABBF405A1A9985256B020060E64E"
name: "OtherDomainServers"

The brackets are not nasty but a correct notation. To access the contens just use [0] in your client side script or with your JSON parser in Java you like.

Perhaps the explanation here can help:

https://quintessens.wordpress.com/2015/05/08/processing-json-data-from-domino-access-services-with-jackson/

Basically you establish a call to DAS via the Jersey client and then you parse the json via Jackson library to a map in java.

During the parsing process you can define which values you want to parse and transform them.

Take a look at the Person class...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM