my $json = ($userID) ?
qq{{"success" : "login is successful", "userid" : "$userID"}} :
qq{{"error" : "username or password is wrong"}};
# return JSON string
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
Ref : http://www.ibm.com/developerworks/cn/webservices/ws-simplelogin/
在 Java 底下 parse JSON 的方式 :
WebClient client = new WebClient();
Page page = client.getPage("http://stackoverflow.com/users/flair/97901.json");
WebResponse response = page.getWebResponse();
if (response.getContentType().equals("application/json")) {
String json = response.getContentAsString();
Map<String, String> map = new Gson().fromJson(json, new TypeToken<Map<String, String>>() {}.getType());
System.out.println(map.get("displayName")); // Benju
}Ref :
http://stackoverflow.com/questions/2932857/html-handling-a-json-response
http://stackoverflow.com/questions/1688099/converting-json-to-java/1688182#1688182