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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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.
welcome, buddy. I was about to write a bunch of code similar to yours below, but you wrote before me ;)
1

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

1 Comment

I tried that as well but Thanks to cihan seven i am able to get my answer. Still thanks for your input.
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.