Skip to content
Advertisement

Parse a String java

I have a BuilderString that contain the same result as in this link: https://hadoop.apache.org/docs/current/hadoop-project-dist/

I’m looking to extract the values of the “. And return a list of String that contain all the files name.

My code is:

try {
    HttpURLConnection conHttp = (HttpURLConnection) url.openConnection();
    conHttp.setRequestMethod("GET");
    conHttp.setDoInput(true);

    InputStream in = conHttp.getInputStream();
    int ch;
    StringBuilder sb = new StringBuilder();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

How can I parse JSON to take all the values of pathSuffix and return a list of string that contains the file names ? Could you please give me a suggestion ? Thanks

Advertisement

Answer

The good option

Do the following steps:

  1. Convert to JSON
  2. Get the value using: JSONObject.get("FileStatuses").getAsJson().get("FileStatus").getAsJsonArray()
  3. Iterate over all objects in the array to get the value you want

The bad option

Although as mentioned it is not recommended- If you want to stay with Strings you can use:

String str_to_find= "pathSuffix"      : "";

while (str.indexOf(str_to_find) != -1){
   str = str.substring(str.indexOf(str_to_find)+str_to_find.length);
   value = str.substring(0,str.indexOf("""));
   System.out.println("Value is " + value);
}

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement