Ant*_*huk 1 java gwt json org.json
我在服务器端制作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库吗?
您正在使用JSONObject JSONVAlue,JSONArray其toString()方法应该为您提供对象的格式良好的json表示.
见:
http://www.gwtproject.org/javadoc/latest/com/google/gwt/json/client/JSONObject.html#toString()
http://www.gwtproject.org/javadoc/latest/com/google/gwt/json/client/JSONValue.html#toString()
http://www.gwtproject.org/javadoc/latest/com/google/gwt/json/client/JSONArray.html#toString()
| 归档时间: | 
 | 
| 查看次数: | 9346 次 | 
| 最近记录: |