2

First of all i am using json-simple-2.1.2.jar[ Link on GitHub ].

It is similar to json-simple-1.1.1.jar but some classes are updated and some some other are deprecated but the logic is the same.


Java code [It produces the below]

 //JSON Array [ROOT]
    JsonArray json = new JsonArray();

    //Libraries Array
    JsonArray libraries = new JsonArray();
    for (int i = 0; i < 2; i++) {
        JsonObject object = new JsonObject();
        object.put("name", "library->" + i);
        libraries.add(object);
    }

    //Add to ROOT ARRAY
    json.add(libraries);

    //Write to File
    try (FileWriter file = new FileWriter(jsonFilePath)) {
        file.write(json.toJson());
        file.flush();
    } catch (IOException e) {
        e.printStackTrace();       
    }

Produced json file:

[
    [
        {
            "name": "library->0"
        },
        {
            "name": "library->1"
        }
    ]
]

What i want:

[   
  "libraries":[
        {
            "name": "library->0"
        },
        {
            "name": "library->1"
        }
    ]
]

As you can see JsonArray has a name for example : "libraries" .

I can't find any way to do this with neither of json-simple.jar i use.

Help is a lot appreciated :)

5
  • what you want is not a valid JSON. Commented Mar 3, 2017 at 5:07
  • @Srikanth A Have a look here tutorialspoint.com/json/json_syntax.htm :) Let me know why you think it is wrong , i am new to JSON . Commented Mar 3, 2017 at 5:19
  • I referred here to confirm the expected JSON is invalid jsonlint.com Commented Mar 3, 2017 at 5:23
  • @Srikanth A Omg i was doing the same thing :) . Strange though why it is not valid ... But in JSON you can have names for Arrays and Objects i mean :) Commented Mar 3, 2017 at 5:24
  • yes, you can have names for Array - But JSON starting is not flower braces. Just change the start and end as Flower braces, it would be a valid JSON Commented Mar 3, 2017 at 5:25

1 Answer 1

3

The expected JSON format expected in question is not a valid JSON. It can be verified here JSONLINT.com

If you replace the starting and angular brackets to Flower brackets, it would be valid JSON. PFB code to build the same.

import org.json.simple.JsonArray;
import org.json.simple.JsonObject;

import java.io.*;

public class Test {

    public static void main(String[] args)
            throws FileNotFoundException {

        //JSON Array [ROOT]
        JsonObject finalOutput = new JsonObject();

        //Libraries Array
        JsonArray libraries = new JsonArray();
        for (int i = 0; i < 2; i++) {
            JsonObject object = new JsonObject();
            object.put("name", "library->" + i);
            libraries.add(object);
        }

        finalOutput.put("libraries", libraries);

        //Write to File
        try (FileWriter file = new FileWriter("C:\\Users\\b21677\\output.json")) {
            file.write(finalOutput.toJson());
            file.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

3 Comments

Thank you veeeery much my friend! :)
Anytime friend :). Refer here json.org for precise idea on JSON.
I would upvote +2 if i could :) One more question.. What is the best JSON Library to use for Java in your opinion?

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.