【发布时间】:2013-05-12 12:21:53
【问题描述】:
我正在尝试将此 json 字符串转换回数组,但我似乎做不到
["\"abba\"","\"repaper\"","\"minim\"","\"radar\"","\"murdrum\"","\"malayalam
\"","\"turrut\"","\"navan\""]
任何人都可以提供帮助,或者为我指出一些教程的正确方向。我试过 split(",") 等,但我真的不太清楚如何自己提取单词。
客户端代码:
Gson gson;
String[] words = { "hello", "Abba", "repaper", "Minim", "radar",
"murdrum", "malayalam", "cheese", "turrut","Navan" };
gson = new Gson();
String json = gson.toJson(words);
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client
.resource("http://localhost:8090/RestSampleApp/rest/webservice/returnarray");
ClientResponse response = service.type(MediaType.APPLICATION_JSON)
.post(ClientResponse.class, json);
String output = response.getEntity(String.class);
//String target2 = gson.fromJson(json, String.class);
System.out.println(output);
网络服务代码:
@POST
@Path("returnarray")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String returnstuff(String list) {
list2 = list.substring(1, list.length() - 1); //gets rid of "[]"
temp = new ArrayList<String>(Arrays.asList(list2.split(",")));
Algorithim algo = new Algorithim(temp); // instance of algorithim class takes in arrayList
algo.getpalindromesarray(); //creates plaindrome arraylist
newlist = algo.getnewlist();
String details = gson.toJson(newlist);
return details;
}
【问题讨论】:
-
不要尝试自己解析。使用 json 库。您的问题被标记为
gson。你在用吗? -
是的,我正在尝试使用 json,但我不确定该怎么做。我将数据作为数组发送到 Web 服务。他们在编辑后重新调整它
-
可以用java函数解析吗?像子串或溢出?在这种情况下它肯定会起作用....我已经多次解析 json 但在你的情况下,斜线在解析时会出现问题
-
这种情况是你发送一个
String[]到一个Web 服务,它返回那个JSON 响应?这正是您从 Web 服务获得的响应吗?这甚至不是有效的 JSON,太多了"! -
尚不清楚为什么您在 JSON 中引用了双引号。使用标准 JSON 解析将导致包含值
"abba"与abba的字符串,例如。
标签: java json parsing jersey gson