0

I am trying to generate a proper JSON output for use with jQuery UI Autocomplete. I am forced to use the JAVA json-simple lib and I tried every combination that I could think of.

Lets suppose that I like the drop down to show a list of "Alex1", "Alex2", "Alex3" etc.

I have tried the following

JSONObject obj =new JSONObject();
List strs = new ArrayList();
    strs.add("Alex1");
    strs.add("Alex2");
    strs.add("Alex3"); 
    strs.add("Alex4");
obj.put("source", strs);
return(obj.toJSONString());

And I have also tried

JSONObject obj =new JSONObject();
Map map = new LinkedHashMap();
    map.put("id1", "Alex1");
    map.put("id2", "Alex2");
    map.put("id3", "Alex3");
    map.put("id4", "Alex4");
obj.put("source", map);
return(obj.toJSONString());

But with no luck

I have tried to return a String made by hand in the proper format and my module works perfectly so I know that the problem is on the JSON output.

an someone tell me how can I set it properly using the json-simple lib??

Thanks

Reporter
  • 3,897
  • 5
  • 33
  • 47
Alexius DIAKOGIANNIS
  • 2,465
  • 2
  • 21
  • 32

2 Answers2

0

You should use JSONArray instead.

JSONObject jobj = new JSONObject();
JSONArray arr = new JSONArray();

arr.add("Alex1");
arr.add("Alex2");

jobj.put("values", arr);

return jobj.toJSONString();

This will return you a JSON string with a key - values having an array of values.

Ajay Kelkar
  • 144
  • 1
  • 5
  • 20
0

Example #1 A json_encode() example

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

The above example will output: {"a":1,"b":2,"c":3,"d":4,"e":5}

Cymbals
  • 1,164
  • 1
  • 13
  • 26