Serialize Json into generic structure without schema using Java and Jackson -


i have need serialize json without being attached particular schema resulting object, e.g., generic set/map/hashmap.

as input, have string json. not know schema json.

as output, want java object such hashmap or similar has key-value serialization of input.

note that input json has both basic fields , array/list inside it.

i have use java , jackson (or other library). how possibly can that?

jackson data binding able read json input map string key , object value (that can map or collection). tell mapper read json map. giving mapper appropriate type reference:

import java.util.*; import com.fasterxml.jackson.core.type.typereference; import com.fasterxml.jackson.databind.objectmapper;  public class test {     public static void main(string[] args)     {         try {             string json = "{ "                     + "\"string-property\": \"string-value\", "                     + "\"int-property\": 1, "                     + "\"bool-property\": true, "                     + "\"collection-property\": [\"a\", \"b\", \"c\"], "                     + "\"map-property\": {\"inner-property\": \"inner-value\"} "                     + "}";              objectmapper mapper = new objectmapper();             map<string, object> map = new hashmap<>();             // convert json string map             map = mapper.readvalue(json, new typereference<map<string, object>>(){});              system.out.println("input: " + json);             system.out.println("output:");             (map.entry<string, object> entry : map.entryset()) {                 system.out.println("key: " + entry.getkey());                 system.out.println("value type: " + entry.getvalue().getclass());                 system.out.println("value: " + entry.getvalue().tostring());             }         } catch (exception e) {             e.printstacktrace();         }     } }    

output:

input: { "string-property": "string-value", "int-property": 1, "bool-property": true, "collection-property": ["a", "b", "c"], "map-property": {"inner-property": "inner-value"} } output: key: string-property value type: class java.lang.string value: string-value key: int-property value type: class java.lang.integer value: 1 key: bool-property value type: class java.lang.boolean value: true key: collection-property value type: class java.util.arraylist value: [a, b, c] key: map-property value type: class java.util.linkedhashmap value: {inner-property=inner-value} 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -