12

I have a model class method which returns a list of objects which contains all the registered user details. I want to fetch the list resturned by all() method and convert the data into JSON object and pass it to the view like a string. How can I do this conversion of this array list to JSON object?

I was unable to do this by below:

ObjectMapper mapper = new ObjectMapper();
JSONObject json = new JSONObject();
JsonNodeFactory jsonnode = JsonNodeFactory.instance;
ObjectNode result = new ObjectNode(jsonnode);
for (int i = 0; i < list.size(); i++) {
    json.put(list.get(i).fname, list.get(i));
    System.out.println(json.get("fname"));
}

@Entity
class Mydata extends Model {

    @Id
    public Long Id;
    public String fname;
    public String lname;
    public String city;
    public String state;
    /****************** READ/select OPERATION *****************/
    public static Finder < Long, Mydata > finder = new Finder(Long.class, Mydata.class);

    public static List < Mydata > all() {
        return finder.all();
    }
    public static void createuser(Mydata user) {
        user.save();
    }
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
pret
  • 273
  • 3
  • 6
  • 18
  • Wait wait wait; you are mixing Jackson and org.json here! – fge Jun 28 '13 at 07:55
  • Possible duplicate http://stackoverflow.com/questions/5813434/trouble-with-gson-serializing-an-arraylist-of-pojos – Jit B Jun 28 '13 at 08:04
  • [conversion of array list to json object string][1] [1]: http://stackoverflow.com/questions/4841952/convert-arraylistmycustomclass-to-jsonarray/30193029#30193029 – dhamat chetan May 12 '15 at 14:01

4 Answers4

19

To convert ArrayList to Json, just download Open Source json utility from: http://json.org/java/ or Jar file from here

And just do:

JSONArray jsonAraay = new JSONArray(your_array_list);

That's it

Note: You should have setter/getter in your POJO/MODEL class to convert arraylist to json

Rakesh Bhalani
  • 668
  • 4
  • 10
1

Don't bother with org.json, use Jackson all the way:

// list is a List<MyData>
final ObjectMapper mapper = new ObjectMapper();
final Map<String, MyData> map = new HashMap<>();
for (final MyData data: list)
    map.put(data.fname, data);
final JsonNode json = mapper.valueToTree(map);
fge
  • 119,121
  • 33
  • 254
  • 329
0

You could use all sorts of third party libraries like others here have suggested, or just use Play's own simplified methods for this (found in play.libs.Json) which works with Jackson objects, but it is integrated into the framework and requires a lot less code to use, for example:

JsonNode myJsonNode = Json.toJson(MyListObject); which converts the List to a JsonNode object, then use something like String jsonResult = Json.stringify(myJsonNode); to convert it into a string representation.

If you are using the JSON in a template, don't forget to wrap it in something like @Html(myJsonString) so it will not escape anything. Otherwise, if you are just outputting the pure JSON to the browser, a simple return ok(jsonResult); will work as Play will automatically set the content type.

Reference link: http://www.playframework.com/documentation/api/2.0/java/play/libs/Json.html

Michael Hawkins
  • 2,793
  • 1
  • 19
  • 33
  • Hi Netizen, i am trying to pass the data from java method (in the form of json array string) to my view. So essentially i am looking for a "JAVA" function to convert List list to json object array .... – pret Jun 29 '13 at 20:58
  • What I posted should do it for you - it converts a Java List object to a JSON string, which is then interpreted in your template by the Javascript interpreter as a JSON array (as long as you did not escape anything like I showed). You can put the resulting JSON data directly into the template or create an AJAX API to feed it as pure text like I showed above. Either way, what I put above should work for you if I understand what you mean. Thanks. – Michael Hawkins Jun 30 '13 at 21:53
0

have you looked at this: http://www.json.org/javadoc/org/json/JSONObject.html#valueToString(java.lang.Object)

JSONObject.valueToString(<<your list of custom object>> OR <<objects>> OR <<map>>)

works just fine...there are some other methods on that lib, if you are interested....

labheshr
  • 2,858
  • 5
  • 23
  • 34