IAM嘗試這個JSON字符串轉換回數組,但我似乎無法做到這一點不能JSON字符串轉換爲普通字符串數組中的Java
["\"abba\"","\"repaper\"","\"minim\"","\"radar\"","\"murdrum\"","\"malayalam
\"","\"turrut\"","\"navan\""]
誰能幫助,或點我在正確的一些教程的方向。我試過分裂(「,」)等,但我真的不太確定如何提取自己的話。
客戶端代碼:
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);
Web服務代碼:
@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'。你在用嗎? – 2013-05-12 12:42:29
是的,我嘗試使用json,但我不知道該怎麼辦。im將數據作爲數組發送到webservice。它們在編輯後重新調用它 – eoin 2013-05-12 12:50:41
可以使用java函數解析它?像子字符串或溢出?它將definatly在這種情況下工作....我已經解析了JSON很多次,但在這裏你的情況斜槓是解析時產生問題 – KOTIOS 2013-05-12 12:54:21