How can I construct a json with arrays and objects in java -


i need json in following structure pass request body service.

{    "i":[      {        "a":{          "o1":"str1";          "o2":234;        }      }    ]  }

i can either pass map <"i[0].a.o1", "str1"> <"i[0].a.02", 345> or pass them string "i[0].a.o1"="str1"&"i[0].a.02"=345

to construct json.

how can convert map or string input json structure above? should use inner classes 'i' , 'a' , use gsonutils.getstring(i)?

i agree answer https://stackoverflow.com/a/45367328/2735286: jackson library provides nice features serializing, deserializing objects , json.

here sample code creates map structure proposed, serializes , deserializes , checks, if deserialized structure same original one.

public class jacksonmappertest {      private static objectmapper mapper = new objectmapper();      public static void main(string[] args) throws ioexception {         map<string, object> root = providemap(); // create map         string json = converttojson(root); // convert string         system.out.println(json); // print json         map<string, object> rootclone = converttomap(json); // convert string map         system.out.println(root.equals(rootclone)); // check, if original , deserialized objects same.     }      private static map<string, object> converttomap(string json) throws ioexception {         typereference<hashmap<string,object>> typeref                 = new typereference<hashmap<string,object>>() {};         return mapper.readvalue(json, typeref);     }      private static string converttojson(map<string, object> root) throws jsonprocessingexception {         return mapper.writerwithdefaultprettyprinter().writevalueasstring(root);     }      private static map<string, object> providemap() {         map<string, object> root = new hashmap<>();         list<map<string, object>> = new arraylist<>();         root.put("i", i);         map<string, object> element = new hashmap<>();         map<string, object> = new hashmap<string, object>() {             {                 put("o1", "str1");                 put("o2", 234);             }         };         i.add(element);         element.put("a", a);         return root;     } } 

if run code see in console:

{   "i" : [ {     "a" : {       "o1" : "str1",       "o2" : 234     }   } ] } true 

if want used jackson in project, can create maven project these dependencies in pom.xml file:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency>     <groupid>com.fasterxml.jackson.core</groupid>     <artifactid>jackson-core</artifactid>     <version>2.8.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency>     <groupid>com.fasterxml.jackson.core</groupid>     <artifactid>jackson-databind</artifactid>     <version>2.8.8</version> </dependency> 

for more infos on jackson, check: https://github.com/fasterxml/jackson


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -