我在服務器端生成JSON字符串。如何在GWT客戶端創建一個JSON字符串?
JSONObject responseObject = new JSONObject();
List<JSONObject> authorList = new LinkedList<JSONObject>();
try {
for (Author author : list) {
JSONObject jsonAuthor = new JSONObject();
jsonAuthor.put("name", author.getName());
jsonAuthor.put("surname", author.getSurname());
authorList.add(jsonAuthor);
}
responseObject.put("authors", authorList);
} catch (JSONException ex) {
ex.printStackTrace();
}
return responseObject.toString();
這就是我如何解析客戶端部分的字符串。
List<Author> auList = new ArrayList<Author>();
JSONValue value = JSONParser.parse(json);
JSONObject authorObject = value.isObject();
JSONArray authorArray = authorObject.get("authors").isArray();
if (authorArray != null) {
for (int i = 0; i < authorArray.size(); i++) {
JSONObject authorObj = authorArray.get(i).isObject();
Author author = new Author();
author.setName(authorObj.get("name").isString().stringValue());
author.setSurname(authorObj.get("surname").isString().stringValue());
auList.add(author);
}
}
return auList;
現在我需要改變雙方的行動。我必須在客戶端上編碼爲JSON並在服務器上解析它,但是我無法看到如何在客戶端上創建JSON字符串,因爲它在服務器上進一步解析。我可以使用標準的GWT JSON庫嗎?