简体   繁体   中英

Obtain specific part of a string with JSON Format java

I have a question on how would be the best way to get the information from a string but that has JSON format.

{
"internal_id":"1234",
"moreInformation":"Failed authentication for user."
}

In this case, I want to get the value of "internal_id" and I already did, with subtring, lastIndexOf and indexOf

public static String returnInternalCode(String json){
    String internalCode = json.substring(json.lastIndexOf("\"internal_id\":\"") + "\"internal_id\":\"".length(), json.length() - 1);
    if (json.lastIndexOf("\"internal_id\":\"") == -1) return null;
    return internalCode.substring(0, internalCode.indexOf("\""));
}

I also tried several JSONs with order changes that don't have the data and it also worked. I leave the full class of tests I did:

public class Test {

    public static void main(String[] args) {
        // Original JSON
        String json = "{\"internal_id\":\"999999\",\"moreInformation\":\"Failed authentication for user, 1 authentication attempt remaining.\"}";

        // Other JSON order
        String json2 = "{\"moreInformation\":\"Failed authentication for user. Invalid response.\", \"moreInformation2\":\"Failed authentication for user. \", \"internal_id\":\"45678\"}";

        // JSON without the internal_id
        String json3 = "{\"moreInformation\":\"Failed authentication for user. Invalid response.\"}";

        // JSON without moreInformation
        String json4 = "{\"internal_id\":\"999999\"}";

        System.out.println("JSON: ".concat(json4));
        System.out.println("internalId: " + returnInternalId(json4));
        System.out.println("moreInformation: " + returnMoreInformation(json4));
    }

    public static String returnInternalId(String json){
        String internalCode = json.substring(json.lastIndexOf("\"internal_id\":\"") + "\"internal_id\":\"".length(), json.length());
        if (json.lastIndexOf("\"internal_id\":\"") == -1) return null;
        return internalCode.substring(0, internalCode.indexOf("\""));
    }

    public static String returnMoreInformation(String json){
        String moreInformation = (json.substring(json.lastIndexOf("\"moreInformation\":\"") + "\"moreInformation\":\"".length(), json.length()));
        if (json.lastIndexOf("\"moreInformation\":\"") == -1) return null;
        return moreInformation.substring(0, moreInformation.indexOf("\""));
    }
}

I would like to know if there are better ways to do what I did, such as with StringBuilder or StringBuffer and also to find out which way uses less memory or is faster to run, how do I know that? How long does it take to execute a method?

Thank you very much!

You can extract the values this way; Using Simple-json library

    JSONObject jobj = (JSONObject) parser.parse(yourJsonString); // Pass the Json formatted String

    String internal_id = (String) jobj.get("internal_id"); // Extract the value from your key

    System.out.println(internal_id); // 1234

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM