0

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'";

svz
  • 23
  • 1

2 Answers2

-1

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.

abstractnature
  • 456
  • 2
  • 9
  • The proper way is to first decode the JSON and then to access the object's content ; not to mangle the string representation, which always leads to issues at some point in the future. – Eric Aya Nov 04 '21 at 12:20
-2
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..

svz
  • 23
  • 1
  • you can see this answer [link](https://stackoverflow.com/questions/24011597/java-8-stream-how-to-return-replace-a-strings-contents-with-a-list-of-items-to) – يعقوب Nov 04 '21 at 16:35
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 04 '21 at 18:10