/* NetRexx */ options replace format comments java crossref symbols nobinary import java.util.List import org.json.JSONObject import org.json.JSONArray import org.json.JSONTokener import org.json.JSONException /** * Using library from json.org * * @see http://www.json.org/java/index.html */ class RJson01 public properties private constant JSON_DWARFS = '' - '{\n' - ' "F1937_1" : {\n' - ' "title" : "Snow White and the Seven Dwarfs",\n' - ' "year" : 1937,\n' - ' "medium" : "film",\n' - ' "dwarfs" : [ "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey", "Doc" ]\n' - ' },\n' - ' "F2012_1" : {\n' - ' "title" : "Mirror, Mirror",\n' - ' "year" : 2012,\n' - ' "medium" : "film",\n' - ' "dwarfs" : [ "Grimm", "Butcher", "Wolf", "Napoleon", "Half Pint", "Grub", "Chuckles" ]\n' - ' },\n' - '}' /** * A bean that looks like the following JSON *
       * {
       *   "F2012_2" : {
       *     "title"  : "Snow White & the Huntsman",
       *     "year"   : 2012,
       *     "medium" : "film",
       *     "dwarfs" : [ "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion" ]
       *   }
       * }
       * 
*/ SAMPLE_BEAN = DwarfBean( - "F2012_2", - "Snow White & the Huntsman", - Long(2012), - "film", - Arrays.asList([String "Beith", "Quert", "Muir", "Coll", "Duir", "Gus", "Gort", "Nion"]) - ) method main(args = String[]) public static say json2bean(JSON_DWARFS) say say bean2json(SAMPLE_BEAN) say return method json2bean(dwarfs) public static returns List say "Make beans from this JSON string:" say dwarfs jsonBeans = ArrayList() do jd = JSONObject(JSONTokener(dwarfs)) ns = JSONObject.getNames(jd) name = String loop name over ns dwarves = ArrayList() jn = jd.getJSONObject(name) title = jn.getString('title') year = Long(jn.getLong('year')) medium = jn.getString('medium') dwa = jn.getJSONArray('dwarfs') loop di = 0 to dwa.length() - 1 dwarves.add(dwa.getString(di)) end di jb = DwarfBean(name, title, year, medium, dwarves) jsonBeans.add(jb.toString()) end name catch ex = JSONException ex.printStackTrace() end return jsonBeans method bean2json(sb = DwarfBean) public static returns String say "Make JSONObject from this bean:" say sb jsonString = String do jd = JSONObject(sb) jo = JSONObject() jo = jo.put(sb.keyGet(), jd) jsonString = jo.toString(2) catch ex = JSONException ex.printStackTrace() end return jsonString -- ============================================================================= class RJson01.DwarfBean public binary properties private key = String -- not part of bean properties indirect title = String year = Long medium = String dwarfs = List method DwarfBean(key_ = String null, title_ = String null, year_ = Long null, medium_ = String null, dwarfs_ = List null) public keyPut(key_) setTitle(title_) setYear(year_) setMedium(medium_) setDwarfs(dwarfs_) return method keyPut(key_ = String) public key = key_ return method keyGet() returns String return key method toString public returns String ts = StringBuilder() ts.append(String.format("%s@%08x ", [Object this.getClass().getSimpleName(), Integer(hashCode())])) ts.append('[') ts.append('key='String.valueOf(keyGet())', ') ts.append('title='String.valueOf(getTitle())', ') ts.append('year='String.valueOf(getYear())', ') ts.append('medium='String.valueOf(getMedium())', ') ts.append('dwarfs='String.valueOf(getDwarfs())) ts.append(']') return ts.toString()