2014-09-03 164 views
0

我有以下字符串如何从以下JSON字符串获取字符串[]?

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

我使用String.split(",")得到的String []。但数组内容包括“[”和““”的。

我需要出去引号,以获得实际的字符串。是否有一个图书馆或方法与我能做到吗?

目前我做的这样的。

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

为什么不尝试解析JSON字符串而不是试图对其进行字符串操作?你会得到一个'List '来解析那个可以很容易地转换为一个字符串数组的JSON字符串。 – SudoRahul 2014-09-03 08:59:40

回答

4

使用boonjackson第三方库将json字符串反序列化为java对象。

宝示例 -

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

查找的Java宝VS杰克逊JSON - 基准 - here

enter image description here

来源:Link

+2

此条形图并不真正有用。它试图说什么?那杰克逊的速度是Boon的25%吗?或者,杰克逊需要25ms的时间才能让Boon需要大约100ms的时间? – Tom 2014-09-03 09:21:11

+0

它是吞吐量图纵轴表示吞吐量。从链接资源中获得更好的理解。 – 2014-09-03 09:40:55

+0

@SubhrajyotiMajumder谢谢。我的项目已经在使用net.sf.json库。我没有找到我可以用同样的方法。 – phoenix 2014-09-03 09:55:16

1

我建议你使用JSON库来解决它。

http://jackson.codehaus.org/

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

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

新的主页在这里:http://wiki.fasterxml.com/JacksonHome :)。 – Tom 2014-09-03 09:02:54

+0

哈哈,谢谢,只是从谷歌搜索它。 – 2014-09-03 09:04:37

1

你可以使用google's gson和你的JSON解码到String[]你可以简单地使用这行代码

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