-3

I tried with the below code to convert List<String> to String[]

    List<String> input = new ArrayList<String>();
    input.add("100");
    input.add("101");
    System.out.println(input);  
    String[] size = new String[input.size()];
    size = input.toArray(size);
    System.out.println(size);

But this one prints as ["100","101"] not as required by me {"100","101"}. . ? I then used GSON to acheive the desired output.

  • Do you want to output JSON or just the string in your example? – Mick Mnemonic Jul 27 '15 at 10:27
  • I need output in JSON format –  Jul 27 '15 at 10:28
  • possible duplicate of [How to convert List to Json in Java](http://stackoverflow.com/questions/14228912/how-to-convert-list-to-json-in-java) – Naman Gala Jul 27 '15 at 10:30
  • Please note that `{"100","101"}` is not JSON, which consists of `"key" : "value"` mappings. For this reason, converting a list/array to JSON is ambiguous; there are no keys in a list, only values. Have a look at the above link by Naman for possible solutions. – Mick Mnemonic Jul 27 '15 at 11:22

1 Answers1

1

In Java the standard output (delegating to toString() method) for arrays is always [items].

If you expect different output, you need to use custom code or library.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40