2

After some researchs, I didn't have found any solutions to this problem: when I create a JSONObject (org.json) from a file, it return "empty":false. Why does it return this and how can I fix it?

Java:

JSONObject config = new JSONObject(Files.readAllLines(Paths.get("config/maj.json")));

JSON:

{"FyloZ":"0"}

Files.readAllLines is working return the right value.

Thanks!

1 Answer 1

7

Files.readAllLines() returns List<String>, not a String.

So actually you are using the following constructor (accepting a single Object parameter):

https://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.Object-

Construct a JSONObject from an Object using bean getters

The only getter-style method of a List is isEmpty(), so you get that 'empty: false' thing.

Try the following:

String json = new String(Files.readAllBytes(Paths.get("config/maj.json")), "utf-8");
JSONObject config = new JSONObject(json);

Here we read JSON as bytes, convert them to a string (assuming it's in utf-8) and then create a JSONObject from it.

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

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.