Java 从较大的子字符串中获取子字符串

Java 从较大的子字符串中获取子字符串,java,json,substring,Java,Json,Substring,我把它作为子串。它是一个JSON字符串。我正在尝试从中获取id字符串。我可以通过使用两个indexOf,然后对两个indexOf进行子串来实现这一点。更好的解决方案是什么 这是我的绳子 "{"id":"762c094a-4b65-499e-b5b2-de34ef8d726e","createdTimestamp":1605558195131,"username":"sssdv",&quo

我把它作为子串。它是一个JSON字符串。我正在尝试从中获取id字符串。我可以通过使用两个indexOf,然后对两个indexOf进行子串来实现这一点。更好的解决方案是什么

这是我的绳子

"{"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}}"
这是我的密码

int id = results.indexOf("id");
        int cr = results.indexOf("createdTimestamp");
        String strId = results.substring(id + 5, cr - 3);

更好的解决方案是使用实际的JSON解析器。外面有很多。从另一个问题上看一下这一点。我建议使用:

更好的解决方案是使用JSON中的字段创建一个类,并将JSON字符串解析到该类:

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
    }
}

为什么不使用Java的JSON库呢?在您定义了“更好”的确切含义之后,我们可以回答可能重复的问题?更少的代码行?是否对字段的重新排序具有鲁棒性?CPU/内存消耗更少?还有什么吗?你应该解释一下你的代码,这样OP和未来的读者都能理解。
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
    }
}
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