java—从较大的子字符串中获取子字符串

ilmyapht  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(434)

我把这个作为子串。它是一个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);
thtygnil

thtygnil1#

更好的解决方案是使用实际的json解析器。外面有很多。从另一个问题来看这个答案。我建议使用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);

更好的解决方案是使用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
    }
}
dgtucam1

dgtucam12#

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

相关问题