【发布时间】:2010-11-09 23:42:35
【问题描述】:
在过去的几天里,我一直试图让 GWT 解释从服务器传回的 JSONValue 或 XML 字符串(使用 PHP)。
我很沮丧,因为我似乎什么也做不了。对于 XML,我已经确认从 PHP 传递到 GWT 的字符串是正确的 XML 字符串。但是,当我尝试解析 XML 时,我得到了一堆空错误。
使用 JSON,我从 PHP 中得到以下信息:
Value: {"item":[{"ID":null, "Primary":null, "Secondary":null, "Date":null, "Region":null},{"ID":null, "Primary":null, "Secondary":null, "Date":null, "Region":null},{"ID":null, "Primary":null, "Secondary":null, "Date":null, "Region":null}]}
我不知道为什么这些值为 NULL,但这是 GWT 查找 JSON 的方式:
public void onChange(Widget sender) {
lb.setText("Date selected: " + calendar.getDate());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String current = df.format(calendar.getDate());
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode("http://www.kbehr.com/calendar/view_duty.php"));
try {
builder.sendRequest(current, new RequestCallback(){
public void onError(Request request, Throwable exception) {
requestFailed(exception);
}
public void onResponseReceived(Request request, Response response) {
String responseText = response.getText();
try {
JSONValue jsonValue = JSONParser.parse(responseText);
processJSON(jsonValue);
} catch (Exception e) {
Window.alert("Error: " + e);
}
}});
}catch (RequestException ex) {
requestFailed(ex);
}
}});
fp.add(calendar);
fp.add(lb);
}
public void processJSON(JSONValue messageXml) {
vp.clear();
vp.add(new Label("Value: " + messageXml));
RootPanel.get("slot2").add(vp);
}
有人知道我对 JSON 做错了什么吗?我在 PHP 中做 json_encode($array),我不知道如何在 GWT 中分解它。
可惜网上的例子也不多……
谢谢!
【问题讨论】: