Converting deeply nested json to java object and vice versa -


i using jaxb json conversion java object. problem facing leads me create huge number of classes serves no purpose except acting place holder json tags.

for example: consider below json:

{ "proposalaggregation": {     "buys": {         "heading1": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         },         "heading2": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         }     },     "sells": {         "heading1": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         },         "heading2": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         }     },     "tachanges": {         "heading1": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         },         "heading2": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         }     },     "existing": {         "heading1": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         },         "heading2": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         }     },     "proposed": {         "heading1": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         },         "heading2": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         }     },     "piachanges": {         "heading1": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         },         "heading2": {             "key1": "value1",             "key2": "value2",             "key3": "value3"         }     } } 

}

now deserialize json as-is java object these java classes need create:

class proposalaggregation --> contains buys, sells, existing, porposed,tachanges, pia  class buys extends calculation class sells extends calculation class existing extends calculation class proposed extends calculation class tachanges extends calculation class piachanges extends calculation  class calculation -- > contains heading1 , heading2  class heading1 class heading2 

so in total 9 classes mimic above json , if looks @ role of these classes nothing place holders json tags. there easy way ?

you can implement google's gson library in following manner. have shown sample class, can modify further.

class - proposalaggregation

package com.test;  import java.util.map;  public class proposalaggregation {  private map<string, map<string, string>> buys; private map<string, map<string, string>> sells; private map<string, map<string, string>> tachanges;  public map<string, map<string, string>> getbuys() {     return buys; }  public void setbuys(map<string, map<string, string>> buys) {     buys = buys; }  public map<string, map<string, string>> getsells() {     return sells; }  public void setsells(map<string, map<string, string>> sells) {     sells = sells; }  public map<string, map<string, string>> gettachanges() {     return tachanges; }  public void settachanges(map<string, map<string, string>> tachanges) {     tachanges = tachanges; }  public string tostring() {     return "buys=" + this.buys + " \nsells=" + this.sells + " \ntachanges="             + this.tachanges; } 

}

test main class note have modified key names verify conversion correct.

package com.test;  import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception;  import com.google.gson.gson;  public class testclass {      public static void main(string[] args) {         gson gson = new gson();          try {              string jsonstring = " { 'buys': { 'bheading1': { 'bkey1': 'value1', 'bkey2': 'value2', 'bkey3': 'value3' }, 'bbheading2': { 'bbkey1': 'value1', 'bbkey2': 'value2', 'bbkey3': 'value3' } }, "                     + "'sells': { 'sheading1': { 'skey1': 'value1', 'skey2': 'value2', 'skey3': 'value3' }, 'ssheading2': { 'sskey1': 'value1', 'sskey2': 'value2', 'sskey3': 'value3' } }, "                     + "'tachanges': { 'heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } }, 'existing': { 'heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } }, 'proposed': { 'heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } }, 'piachanges': { 'heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } } }";               // convert java class             proposalaggregation obj = gson.fromjson(jsonstring, proposalaggregation.class);             system.out.println("object : " + obj);              // convert json             string jsonstringfromobj = gson.tojson(obj);             system.out.println("json : " + jsonstringfromobj);          } catch (exception e) {             e.printstacktrace();         }      }  } 

here's output.

object : buys={bheading1={bkey1=value1, bkey2=value2, bkey3=value3}, bbheading2={bbkey1=value1, bbkey2=value2, bbkey3=value3}}  sells={sheading1={skey1=value1, skey2=value2, skey3=value3}, ssheading2={sskey1=value1, sskey2=value2, sskey3=value3}}  tachanges={heading1={key1=value1, key2=value2, key3=value3}, heading2={key1=value1, key2=value2, key3=value3}} json : {"buys":{"bheading1":{"bkey1":"value1","bkey2":"value2","bkey3":"value3"},"bbheading2":{"bbkey1":"value1","bbkey2":"value2","bbkey3":"value3"}},"sells":{"sheading1":{"skey1":"value1","skey2":"value2","skey3":"value3"},"ssheading2":{"sskey1":"value1","sskey2":"value2","sskey3":"value3"}},"tachanges":{"heading1":{"key1":"value1","key2":"value2","key3":"value3"},"heading2":{"key1":"value1","key2":"value2","key3":"value3"}}} 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -