0

I'm trying to convert the following script into java json, however I'm failing at "aTargets", [0]

I'm not sure how to create an array with just zero.

Script to be converted,

var oTable = $('#example').dataTable( {
        "aoColumnDefs": [
            { "bSortable": false, "aTargets": [ 0 ] }
        ],
        "aaSorting": [[1, 'asc']]
});

My Java

 public JSONObject getOptions() {
        JSONObject json = new JSONObject();
        json.put("aoColumnDefs", new JSONArray()
                //Failing here
                .put(new JSONObject("bSortable", "false", "aTargets", "[0]")));
                                                          //Failing here too
        json.put("aaSorting", new JSONArray(new JSONArray(1, 'asc')));
        return json;
    }
4
  • 2
    I don't think that is how you use JSONObject. See here json.org/javadoc/org/json/JSONObject.html for constructor summary. Commented Feb 8, 2013 at 20:54
  • 1
    What you have in your first script block is not a "JSON object." It is JavaScript using literal syntax. JSON is a serialization scheme using a subset of JavaScript literal syntax. Commented Feb 8, 2013 at 21:04
  • Thanks Sotirios, I completely over looked my code. Commented Feb 8, 2013 at 21:06
  • Ah, Thanks JAAulde, I didn't realize there was a difference. Commented Feb 8, 2013 at 21:07

1 Answer 1

1

This is untested but I believe it's correct:

JSONObject json = new JSONObject();
json.put("aoColumnDefs", new JSONArray()
  .put(new JSONObject("bSortable", "false", "aTargets", new JSONArray().put(0))));
json.put("aaSorting", new JSONArray().put(new JSONArray().put(1, "asc"));
Sign up to request clarification or add additional context in comments.

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.