working with GSON

working with GSON

ashiersashiers Posts: 101Questions: 8Answers: 7
edited May 2013 in Editor
Hi Guys,

I work with Java as my language of choice and the JSON engine I'm working with is called GSON, which was created by Google. With GSON I've managed to replicate a typical JSON string such as the one in example: http://editor.datatables.net/release/DataTables/extras/Editor/examples/join.html

I'm at a point where I'm trying to insert additional data on the end of the JSON string such as in the example where you are returning to the client side the options for "dept" and "access":

... "dept":[{"value":"1","0":"1","label":"IT","1":"IT"},{"value":"2","0":"2","label":"Sales","1":"Sales"},{"value":"3","0":"3","label":"Pre-Sales","1":"Pre-Sales"},{"value":"4","0":"4","label":"Marketing","1":"Marketing"},{"value":"5","0":"5","label":"Senior Management","1":"Senior Management"},{"value":"6","0":"6","label":"Accounts","1":"Accounts"},{"value":"7","0":"7","label":"Support","1":"Support"}],"access":[{"value":"1","0":"1","label":"Printer","1":"Printer"},{"value":"2","0":"2","label":"Servers","1":"Servers"},{"value":"3","0":"3","label":"Desktop","1":"Desktop"},{"value":"4","0":"4","label":"VMs","1":"VMs"},{"value":"5","0":"5","label":"Web-site","1":"Web-site"},{"value":"6","0":"6","label":"Accounts","1":"Accounts"}]}

If we look at the key:value pairs "value":"1" and "label":"IT", I understand that these and their values are derived from some table in the database, which is fine, and I can certainly create a class that has those named as variables: value and label. However, you have two other key:value pairs in your string: "0":"1" and "1":"IT". The key portion of these pairs are numeric in both cases. This is a problem in Java in that I can't name a variable starting with a numeric value. It has to be an alpha character. The values they contain are actually redundant since the value and label keys contain the same values. For DataTables, do the "0" and "1" keys have to be numeric, or can they be labelled something else? I'm hoping for the latter. Please advise.

Alan

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    That's actually kind of an error on my part. The numeric values are irrelevant and a side effect of getting both an associative array of values from the database, and a numerical array. The 0 and 1 are just another form of value and label (respectively).

    They aren't needed :-)

    I'll look at tidying up the libraries for this. Thanks for pointing this out.

    Allan
  • ashiersashiers Posts: 101Questions: 8Answers: 7
    Wonderful, that makes my life a lot easier.
    Thanks,

    Alan
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    I'm all about an easy life ;-)

    Allan
This discussion has been closed.