-5

// My property file :

   key1 = value1
   key2 = value2,value3
   key3 = value4, value5, value6

Requirement in json
  "Arguments":  
   {  
     "key1":"value1",
      "key2":["value2","value3"]   
      "key3":["value4","value5","value6"]
   }  

// Please provide me the java code

Ramesh
  • 1
  • 1
  • 3
    Of course. Do you want coffee with the code too ? – Stéphane Ammar Jan 05 '18 at 08:46
  • First you need to mention the problem you're trying to solve How do I ask a good question? https://stackoverflow.com/help/how-to-ask – Mizuki Jan 05 '18 at 08:47
  • The problem is how to put the multiple values of a key to a json object. Like key2 and key3 in above example. Single value we can do. – Ramesh Jan 05 '18 at 09:12

1 Answers1

0

Put your "Arguments" into a class, I'm going to call it Argument. Then use Gson to serialise this class to JSON.

Like the following:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String response = gson.toJson(argumentInstance);
Files.write(path, Arrays.asList(response.split("\n")));

You can then read it back like so, providing the class:

Gson gson = new Gson();
argumentInstance = gson.fromJson(Files.newBufferedReader(path), Argument);
ifly6
  • 5,003
  • 2
  • 24
  • 47