java - Not able to build a JSON object correctly using JSONObject -


i have following program building json object. not sure how build array of arrays using following program.

pom.xml

<dependency>     <groupid>com.googlecode.json-simple</groupid>     <artifactid>json-simple</artifactid>     <version>1.1.1</version> </dependency> 

jsonobjectconverter.java

public class jsonobjectconverter {      private static final string store_id = "tmsus";      public static void main(string[] args) {         system.out.println(print1());     }      private static string print1() {         jsonobject body = new jsonobject();          jsonarray events1 = new jsonarray();         events1.add(100l);         events1.add(200l);         jsonarray events2 = new jsonarray();         events2.add(300l);         events2.add(400l);          jsonarray eventlogs = new jsonarray();         eventlogs.add(events1);         eventlogs.add(events2);          body.put("storeid", store_id);         body.put("eventlogs", eventlogs);          return body.tostring();     }  } 

output current program:

{   "eventlogs": [     [       100,       200     ],     [       300,       400     ]   ],   "storeid": "tmsus" } 

expected output:

{   "eventlogs": [     {       "storeid": "tmsus",       "eventids": [         100,         200       ]     },     {       "storeid": "tmsca",       "eventids": [         300,         400       ]     }   ],   "username": "meonly" } 

not sure how expected output.

please guide.

never mind, got working. here updated method.

 private static string print1() {         jsonobject body = new jsonobject();          jsonobject eventlog1 = new jsonobject();         jsonarray events1 = new jsonarray();         events1.add(100l);         events1.add(200l);         eventlog1.put("storeid", "tmsus");         eventlog1.put("eventids", events1);          jsonobject eventlog2 = new jsonobject();         jsonarray events2 = new jsonarray();         events2.add(300l);         events2.add(400l);         eventlog2.put("storeid", "cbkus");         eventlog2.put("eventids", events2);          jsonarray eventlogs = new jsonarray();         eventlogs.add(eventlog1);         eventlogs.add(eventlog2);          body.put("eventlogs", eventlogs);         body.put("username", "customer-portal-user");          return body.tostring();     } 

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 -

Add new key value to json node in java -