-2

I have this as a substring. It is a JSON string. I am trying to get the id string from it. I was able to do this by using two indexOf's and then substring the two indexOf's. What is a better solution.

Here is my string

"{"id":"762c094a-4b65-499e-b5b2-de34ef8d726e","createdTimestamp":1605558195131,"username":"sssdv","enabled":false,"totp":false,"emailVerified":false,"firstName":"cdf","lastName":"dddz","email":"hgddf@fdaddf.com","disableableCredentialTypes":[],"requiredActions":[],"notBefore":0,"access":{"manageGroupMembership":true,"view":true,"mapRoles":true,"impersonate":true,"manage":true}}"

And here is my code.

int id = results.indexOf("id");
        int cr = results.indexOf("createdTimestamp");
        String strId = results.substring(id + 5, cr - 3);
Aaron
  • 4,380
  • 19
  • 85
  • 141

2 Answers2

1

A better solution is to use an actual JSON parser. There are plenty out there. Take a look at this answer on a different question. I would suggest using Gson:

String json = "{\"id\":\"762c094a-4b65-499e-b5b2-de34ef8d726e\",\"createdTimestamp\":1605558195131,\"username\":\"sssdv\",\"enabled\":false,\"totp\":false,\"emailVerified\":false,\"firstName\":\"cdf\",\"lastName\":\"dddz\",\"email\":\"hgddf@fdaddf.com\",\"disableableCredentialTypes\":[],\"requiredActions\":[],\"notBefore\":0,\"access\":{\"manageGroupMembership\":true,\"view\":true,\"mapRoles\":true,\"impersonate\":true,\"manage\":true}}";
Gson gson = new GsonBuilder().setPrettyPrinting().create(); // Create the Gson instance
JsonElement element = gson.fromJson(json, JsonElement.class); // Parse it
String id = element.getAsJsonObject().get("id").getAsString(); // Get your desired element
System.out.println(id);

An even better solution would be to create a class with the fields from your JSON and parse the JSON string to that class:

public class MyObject {
    
    // The names and types of these fields must match the ones in your JSON string
    private String id, username, firstName, lastName, email;
    private long createdTimestamp;
    private boolean enabled, totp, emailVerified;
    private String[] disableableCredentialTypes, requiredActions;
    private int notBefore;
    private Access access;

    public String getId() {
        return id;
    }
   
    // Other getters and setters...

    private static class Access {
        private boolean manageGroupMembership, view, mapRoles, impersonate, manage;
        // ...
    }

    public static void main(String[] args) throws IOException {
        String json = "{\"id\":\"762c094a-4b65-499e-b5b2-de34ef8d726e\",\"createdTimestamp\":1605558195131,\"username\":\"sssdv\",\"enabled\":false,\"totp\":false,\"emailVerified\":false,\"firstName\":\"cdf\",\"lastName\":\"dddz\",\"email\":\"hgddf@fdaddf.com\",\"disableableCredentialTypes\":[],\"requiredActions\":[],\"notBefore\":0,\"access\":{\"manageGroupMembership\":true,\"view\":true,\"mapRoles\":true,\"impersonate\":true,\"manage\":true}}";
        Gson gson = new GsonBuilder().setPrettyPrinting().create(); // Create the Gson instance
        MyObject object = gson.fromJson(json, MyObject.class); // Parse the string to your data type
        System.out.println(object.getId()); // Print the id
    }
}
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
0
String results = "{\"id\":\"762c094a-4b65-499e-b5b2-de34ef8d726e\",\"createdTimestamp\":1605558195131,\"username\":\"sssdv\",\"enabled\":false,\"totp\":false,\"emailVerified\":false,\"firstName\":\"cdf\",\"lastName\":\"dddz\",\"email\":\"hgddf@fdaddf.com\",\"disableableCredentialTypes\":[],\"requiredActions\":[],\"notBefore\":0,\"access\":{\"manageGroupMembership\":true,\"view\":true,\"mapRoles\":true,\"impersonate\":true,\"manage\":true}}";
   
String[] parts = results.split("\"");
 
System.out.println(parts[3]); //gives the id, every time
Cardinal System
  • 2,749
  • 3
  • 21
  • 42