0

Hi i have a problem regarding the merge of JSONArray inside JSONObject. Below is what my JSONObject looks like:

{
 "name":"sample.bin.png",
 "coords":{
           "1":{"x":[ 974, 975],"y":[154, 155},
           "3":{"x":[124, 125],"y":[529]},
           "8":{"x":[2048, 2049],"y":[548, 560, 561, 562, 563, 564 ]}
          }
 }

Now i have keys of those JSONObjects which i want to merge (inside coords).I wanted to merge x and y respectively into one JSONObject here is my code:

     String[] tokens = request().body().asFormUrlEncoded().get("coords")[0].split(","); //here i recieve the String Array Keys of the coords i want to merge
        if (!image.equals("")) {
            JSONObject outputJSON = getImageJSON(image); //here comes the JSON which i posted above
            JSONObject coordsPack = (JSONObject) outputJSON.get("coords");
            JSONObject merged = new JSONObject();
            merged.put("x", new JSONArray());
            merged.put("y", new JSONArray());
            for (String index : tokens) {
                JSONObject coordXY = (JSONObject) coordsPack.get(index);
                JSONArray xList = (JSONArray) coordXY.get("x");
                JSONArray yList = (JSONArray) coordXY.get("y");
                merged.get("x").addAll(xList);
                merged.get("y").addAll(yList);
            }
            System.out.println(merged);
        }

but problem is that i am having error at merged.get("x").addAll(xList); and merged.get("y").addAll(yList); i am unable to access the methods.

Seeker
  • 1,877
  • 4
  • 32
  • 56

3 Answers3

1

You must fill the lists first, and you should take out these following lines out of for loop.

        merged.get("x").addAll(xList);
        merged.get("y").addAll(yList);

BTW, it's apoor design to achieve your goal.

cihan adil seven
  • 541
  • 1
  • 4
  • 20
  • Thanks for highlighting i actually am able to merge them as per your suggestion. Sorry i am new to Java so i am not sure about what could be best approach any suggestion on what could be best approach? – Seeker May 02 '16 at 11:33
  • In java, structure of collections are quite different from php. You should be precise about the data type you are using and be careful about the scopes of certain code blocks. I'll update my answer to make an approach to your goal. – cihan adil seven May 02 '16 at 11:44
  • I see thanks a bunch mate. I will try to look into datatypes of Java for more details about what you have mentioned. Thanks again. – Seeker May 02 '16 at 11:45
  • 1
    welcome, buddy. I was about to write a bunch of code similar to yours below, but you wrote before me ;) – cihan adil seven May 02 '16 at 11:49
1

Don't you need to cast it into JSONArray class first, like you did for the 2 lines above?

Minh Kieu
  • 475
  • 3
  • 9
  • I tried that as well but Thanks to cihan seven i am able to get my answer. Still thanks for your input. – Seeker May 02 '16 at 11:38
0

As per suggestion of @cihan seven i am able to get the answer of my problem here is my solution:

        JSONObject coordsPack = (JSONObject) outputJSON.get("coords");
        JSONObject merged = new JSONObject();
        JSONArray xList = new JSONArray();
        JSONArray yList = new JSONArray();
        for (String index : tokens) {
            JSONObject coordXY = (JSONObject) coordsPack.get(index);
            xList.addAll((JSONArray) coordXY.get("x"));
            yList.addAll((JSONArray) coordXY.get("y"));
        }
        merged.put("x", xList);
        merged.put("y", yList);
        System.out.println(merged);
Seeker
  • 1,877
  • 4
  • 32
  • 56