2

I am using json simple

this is my code:

    public static String getDetails() {
    String name = System.getProperty("user.name");
    JSONParser parser = new JSONParser();

    File dir = new File("C:\\Users\\" + name + "\\AppData\\Roaming\\.minecraft\\launcher_profiles.json");

    if (dir.exists()) {
        Object obj = null;
        try {
            obj = parser.parse(new FileReader("C:\\Users\\" + name + "\\AppData\\Roaming\\.minecraft\\launcher_profiles.json"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = (JSONObject) obj;
        String da = (String) jsonObject.get("username");

        try {
            return obj.toString() + "\n" + da;

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("dir no exist");
    }
    return  null;
}

when i print this out it returns all the text in the json file and it returns null from String 'da' I dont know why because its not null it exist in the file??

JSON file: https://hastebin.com/sirefacado.json

2
  • 5
    Post the JSON we can't help you unless you provide all needed information. 'username' is not part of the launcher_profiles.json (based on latest minecraft), look at the actual json file. Commented Mar 12, 2018 at 20:09
  • hastebin.com/sirefacado.json here is the json file Commented Mar 13, 2018 at 6:49

3 Answers 3

2

To access the username you have to use the fully qualified path. In your case that is:

String da = (String) ((JSONObject)((JSONObject)jsonObject.get("authenticationDatabase")).get("d46e53840f3f41a2b9e44e2d4d72ebeb")).get("username");

That is, because your username is encapsulated in the following part of the JSON file:

authenticationDatabase: {
  d46e53840f3f41a2b9e44e2d4d72ebeb: {
    accessToken: "86ccdfsdfsdfsc2c38ec6012a1ccfsdfR",
    username: "[email protected]",
    profiles: {
      ad4fa7102fb7432cb4e07d471e348c77: {
        displayName: "hio"
      }
    }
  }
}

To access the username via the token you have to go via the authenticationDatabase. It may be the case that there are multiple ids, therefore you have to iterate over all the existing ones For that you can do

JSONObject authDatabase = (JSONObject) jsonObject.get("authenticationDatabase");
for(Object id : authDatabase.keySet()) {
  JSONObject authEntry = (JSONObject) authDatabase.get(id);
  String username = (String) authDatabase.get("username");
  /* now do something with the username. 
     You can abort after you found the first username 
     and store it in the da object, or create a list
     of existing usernames, ... */
}
Sign up to request clarification or add additional context in comments.

4 Comments

but like authenticationDatabase. and then the numbers are different so i cant just put that in how would i do that?
You can retrieve the JSONOBject for the authenticationDatabase by doing something like JSONObject jsonObject = (JSONObject) jsonObject.get("authenticationDatabase");
Is this fully qualified path actually works? I tried with json-simple-1.1 and simply not giving the expected result.
You're correct, it doesn't work. I updated the answer
0

Following are the top level keys in the JSON you posted.

{
    "settings": {...some data...},
    "launcherVersion": {...some data...},
    "clientToken": "dbf69db062d5d32b093e7d67ce744d60",
    "profiles": {...some data...},
    "analyticsFailcount": 0,
    "analyticsToken": "f18d7c0f152f5ad44b2a6525e0d5cfa9",
    "selectedProfile": "OptiFine",
    "authenticationDatabase": {...some data...},
    "selectedUser": {...some data...}
}

Your code tries to extract the value of username from the top level.

String da = (String) jsonObject.get("username")

It doesn't contain the key username. Hence, it is print null.

Comments

0

Your statement below is trying to fetch an element named "username" from the root JSON object however, your actual value is nested inside.

String da = (String) jsonObject.get("username");

{
    "authenticationDatabase": {
        "d46e53840f3f41a2b9e44e2d4d72ebeb": {
            "accessToken": "86ccdfsdfsdfsc2c38ec6012a1ccfsdfR",
            "username": "[email protected]",
            "profiles": {
                "ad4fa7102fb7432cb4e07d471e348c77": {
                    "displayName": "hio"
                }
            }
        }
    }
}

In order to fetch inner element, you need to drill down as follows. Understand that it is not an good idea to hard cord the key inside the "authenticationDatabase" object.

    JSONObject jsonObject = (JSONObject) obj;
    String da;
    try {
        JSONObject adb = (JSONObject) jsonObject.get("authenticationDatabase");
        JSONObject adbKey = null;
        for(Object key:adb.keySet()) {
            String sKey = (String) key;
            adbKey = (JSONObject) adb.get(sKey);
            da = (String) adbKey.get("username");
            return obj.toString() + "\n" + da;                  
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

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.