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();
    }
}
3

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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

2 Comments

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<MyData> list to json object array ....
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.
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....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.