The following code is in PHP. I want to do the same in Java. Please tell me how do I generate this type of array or collection in Java. I need this to response to JSON autocomplete.
<?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. 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:-
$('#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. 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. Please tell me how can I get this...
In Java, you would use a 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:-
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:-
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.