Home > Software design >  Reading file json and convert to string and replace chars in java8
Reading file json and convert to string and replace chars in java8

Time:11-05

How read file and convert from json to string? Example: i have file numbers.json [ "23423423", "234234234", "53453453" ]

And i want to have: String numbers = "'23423423', '234234234', '53453453'";

CodePudding user response:

Here is the code, without using any kind of library, (assuming you only have json in form of single arrays like you have.

String json = "[\"23423423\", \"234234234\", \"53453453\" ]"
String formattedJson = json.replace("[", "").replace("]", "").replace("\"", "").replace(" ", "");
String [] nums = formattedJson.split(",");

And you will have your numbers in the nums[] array.

CodePudding user response:

Path path = Paths.get(".../.../numbers.json");
    List<String> listString= Files.readAllLines(path);
    String result = listString.stream()
            .map(String::valueOf)
            .collect(Collectors.joining(""));

    result = result.replace("\"", "\'");
    result = result.replace("[", "");
    result = result.replace("]", "");
    return result;

this is how i code this, but i know, it looks so bad..

  • Related