[英]Servlet response data for autocomplete
The following code is in PHP. 以下代码在PHP中。 I want to do the same in Java. 我想在Java中做同样的事情。 Please tell me how do I generate this type of array or collection in Java. 请告诉我如何在Java中生成这种类型的数组或集合。 I need this to response to JSON autocomplete. 我需要它来响应JSON自动完成。
<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$items = array(
"Peter Pan"=>"peter@pan.de",
"Molly"=>"molly@yahoo.com",
"Forneria Marconi"=>"live@japan.jp",
"Master Sync"=>"205bw@samsung.com",
"Dr. Tech de Log"=>"g15@logitech.com",
"Don Corleone"=>"don@vegas.com",
"Mc Chick"=>"info@donalds.org",
"Donnie Darko"=>"dd@timeshift.info",
"Quake The Net"=>"webmaster@quakenet.org",
"Dr. Write"=>"write@writable.com"
);
$result = array();
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
array_push($result, array(
"name" => $key,
"to" => $value
));
}
}
echo json_encode($result);
?>
I want java version of this PHP code because this code is returning in JSON format. 我想要此PHP代码的Java版本,因为此代码以JSON格式返回。 In 在
{name=>"Peter Pan",
to=>"peter@pan.de";
.....}
As you see this:- 如您所见:
array_push($result, array(
"name" => $key,
"to" => $value
));
Which can be handled by this jQuery code:- 可以通过以下jQuery代码处理:
$('#inputItem').autocomplete('<c:url value="/json/itemautocomplete.do" />', {
multiple: true,
mustMatch: true,
autoFill: true,
highlight: false,
scroll: true,
dataType: "json",
parse: function(data){
var array = new Array();
for(var i = 0; i<data.length; i++){
array[array.length] = {data: data[i], value: data[i].name, result: data[i].name};
}
return array;
}
});
This plugin is available on this url 此插件可在此网址上找到
I know how to handle JSON data by using JSONArray
in $.getJSON
jQuery method. 我知道如何通过在$.getJSON
jQuery方法中使用JSONArray
处理JSON数据。 But that thing is not working in this case. 但是在这种情况下,该功能不起作用。 I think I need to format my data as I described above in this answer so that this jQuery autocomplete plugin can understand the data. 我想我需要按照上述答案中的说明格式化数据,以便此jQuery自动完成插件可以理解数据。 Please tell me how can I get this... 请告诉我我怎么能得到这个...
In Java, you would use a Map<String, String>
: 在Java中,您将使用Map<String, String>
:
Map<String, String> items = new HashMap<String, String>();
items.put("Peter Pan", "peter@pan.de");
String petersAddress = items.get("Peter Pan");
You can iterate through the keyset: 您可以遍历键集:
for ( String key : items.keySet() ) {
if ( key.toLowerCase().startsWith(input) ) {
//add to list of potential matches
}
}
Thanks for ur support. 感谢您的支持。
I have handled the data by using this code:- In Servlet:- 我已经使用以下代码处理了数据:-在Servlet中:-
LinkedList arr = new LinkedList();
arr.add("Peter Pan <peter@pan.de>");
arr.add("Molly <molly@yahoo.com>");
arr.add("Forneria Marconi <live@japan.jp>");
Iterator iter = arr.iterator();
while(iter.hasNext()){
out.println(iter.next());
}
In JQuery:- 在JQuery中:
function itemAutocomplete(){
$('#inputItem').autocomplete('<c:url value="/json/itemautocomplete.do?mi=' + $('#sltMainItem').val() + '&si=' + $('#sltSubItem').val() + '" />', {
json: true
});
}
Thanks for being here for me Shams 感谢您在我身边
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.