0

as the Title states i would like to check the Keys of an external Json File in Java.

What i tried:

JSONObject json = new JSONObject("src/com/json/inventory.json");
System.out.println(json.keys());

Issue: Doesn't do anything.

What are other posibilities?

Thank you in advance!

EDIT Error

  • What does `Doesn't do anything` mean ? – Saif Asif Feb 18 '20 at 16:43
  • What is the full class name of your JSONObject ? – Alice Oualouest Feb 18 '20 at 16:45
  • Gives out an Error and doesn't function –  Feb 18 '20 at 16:49
  • What do you mean with full class name? JSONObject json = new JSONObject("src/com/json/inventory.json"); i tried to add the path to the invetory.json - which is the .JSON File –  Feb 18 '20 at 16:50
  • 2
    Please edit your post and add the error stack that is printed when you execute these lines. By the way `Doesn't do anything` and `Gives out an error` isn't the same thing at all. – Imaguest Feb 18 '20 at 16:57
  • 1
    JSONObject doesn't handle a simple String path like that. The error you get is because the constructor `JSONObject(String)` waits for a serialized JSON, not a file path. That's why the exception says : must begin with '{' – Imaguest Feb 18 '20 at 17:06
  • 1
    It's not looking at the contents of the file. It's parsing the file path you've given it as if _that_ was json. Look at the API documentation for the `JSONObject` you're using (which is possibly why @AliceOualouest was asking for the full class name above) to find a way to do it via files (or load the file contents to a `String` using any of the normal File IO). – BeUndead Feb 18 '20 at 17:07

2 Answers2

0

If you're using this library, first you should read the file content like:

String unparsedJson = Files.readAllLines(Paths.get("src/com/json/inventory.json"))
                .stream()
                .reduce((a,b) -> a + b)
                .get();

Then you can parse json with the code you provided:

JSONObject json = new JSONObject(unparsedJson);

And iterate over the keys with something like this:

json.keys().forEachRemaining(key -> {
            System.out.println(key);
        });

You could also use other libraries such as Jackson and Gson.

Mkay
  • 1
  • 1
  • Hey, yes i do indeed Use org.json. There seem to come out an Error : "java.nio.charset.MalformedInputException: Input length = 1" at the: String unparsedJson = Files.readAllLines(Paths.get("src/com/json/inventory.json")) - Line –  Feb 19 '20 at 13:28
  • 1
    It seems there are some issues with the file encoding; take a look at [this thread](https://stackoverflow.com/questions/26268132/all-inclusive-charset-to-avoid-java-nio-charset-malformedinputexception-input). – Mkay Feb 19 '20 at 22:33
0
  1. Get JSON file from Assets Folder
  2. Print all keys from JSON

-

private String getJSonFromAssets(){
    String json = null;
    try {
        InputStream is = getAssets().open("colorAndValue.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }
    return json;
}

---

try {
        JSONObject jsonObject = new JSONObject(getJSonFromAssets());
        Iterator<String> keys = jsonObject.keys();
        while(keys.hasNext()) {
            String key = keys.next();
            System.out.println(key);
            Log.d("innerKey", "key " + key);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And your output will be like 1. key color0 2. key color1 3. key color2 4. key color3 5. key color4 6. key color5

CHINNA CHARY
  • 21
  • 1
  • 7
  • There seem to be an Error getAsses().open - Method isn*t recognized. What Library do you use? –  Feb 19 '20 at 13:30