phoenix
phoenix

Reputation: 995

How to get a string[] from the following JSON string?

I have the following string

["[email protected]","[email protected]"]

I am using String.split(",") to get String[]. But array contents consist of '[' and '"'.

I need to get the actual strings with out quotes. Is there a library or method with which I can do it?

At present I am doing like this.

    recipients = recipients.replace("\"", "");
    recipients = recipients.replace("[", "");
    recipients = recipients.replace("]", "");
    String[] totalRecipients = recipients.split(",");

Upvotes: 0

Views: 1754

Answers (3)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41220

De-serialize the json string to java object using boon or jackson 3rd party library.

Boon Example -

ObjectMapper mapper =  JsonFactory.create();
String[] recipientArray = mapper.readValue(recipients , String[].class, String.class);

Find Java Boon vs jackson json - Benchmarks - here

enter image description here

Source : Link

Upvotes: 4

Ker p pag
Ker p pag

Reputation: 1588

you can use of google's gson and to decode your json to String[] you can simply use this line of code

Gson gson = new Gson();
String[] myArray =  gson.fromJson(yourjson,String[].class);

Upvotes: 1

Jeff Lee
Jeff Lee

Reputation: 781

I suggest you to use json library to solve it.

http://jackson.codehaus.org/

String s = "[\"[email protected]\",\"[email protected]\"]";

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(s);
for(JsonNode n : node){
 .......
}

Upvotes: 1

Related Questions