我想要使用json对象的JSon响应 它正在使用gson,但我想使用jsonobject我是jquery中的新手 我希望使用json objec
以下代码中遗漏了哪些内容我试过这个automcomplete在使用json对象之后尝试完成
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
String[] strarray = fetchcity().toArray(new String[0]);
JSONArray arrayObj = new JSONArray();
String query = request.getParameter("term");
for (int i = 0; i < strarray.length; i++) {
if (strarray[i].toLowerCase().startsWith(query.toLowerCase())) {
arrayObj.add(strarray[i]);
}
}
out.print(arrayObj.toString());
out.close();
下面是使用json对象现在解决但问题是
client side this don't work
response.setContentType("application/json");
final String param = request.getParameter("term");
final List<AutoCompleteData> result = new ArrayList<AutoCompleteData>();
for (final String country : fetchcity()) {
if (country.toLowerCase().startsWith(param.toLowerCase())) {
result.add(new AutoCompleteData(country, country));
}
}
JSONObject json = new JSONObject();
json.put("geonames",result);
客户端......
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$("#country").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://localhost:8080/FirstTestServlet/Ajaxservlet",
type: "GET",
data: { term: request.term },
dataType: "json",
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
}
});
});
});</script>
</head>
<body>
<form>
<div class="ui-widget">
<label for="country">Country: </label>
<input id="country" />
</div>
</form>
</body>