java - Adding several JSONObject into one but not as an array -
i working on java api , need format data json. want following structure :
{ "main": { "point1": { "x": 0.18, "y": 10.8, "z": 0 }, "point2": { "x": 0.18, "y": 9.36, "z": 0 }, "point3": { "x": 0.18, "y": 8.46, "z": 0 }, "point4": { "x": 0.18, "y": 7.38, "z": 0 } } }
basically there list of points in "main" jsonobject object don't want array.
jsonobject main = new jsonobject(); for(point p : points){ jsonobject point = new jsonobject(); jsonobject coordinates = new jsonobject(); coordinates.put("x", p.getx()); coordinates.put("y", p.gety()); coordinates.put("z", p.getz()); point.put(p.getname(),coordinates); main.put("main", point); }
i following result code above :
{ "main": { "point4": { "x": 0.18, "y": 7.38, "z": 0 } } }
the put method use on last line overwritting previous points, got 1 point in main object @ end. guess solution problem trivial i'm unable find it.
me ?
you there.
accumulate points single json object adding each point separate field:
jsonobject main = new jsonobject(); list<point> points = new arraylist<>(); points.add(new point("point1", 1, 2, 3)); points.add(new point("point2", 2, 3, 4)); jsonobject pointsasjson = new jsonobject(); (point p : points) { jsonobject coordinates = new jsonobject(); coordinates.put("x", p.getx()); coordinates.put("y", p.gety()); coordinates.put("z", p.getz()); pointsasjson.put(p.getname(), coordinates); } main.put("main", pointsasjson);
wiki
Comments
Post a Comment