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);
?>
Update:
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>:You can iterate through the keyset: