开发者

Javascript to Java using JSON

I am planning to send Javascript array object filled with data by encoding it in JSON. Any good jquery plugins for this ?

On Java end, what is the standard way of parsing the JSON into a Java array ?

Overall flow is like this:

  1. Javascript compiles bunch of data on current page and stores it in array.
  2. Array object encoded into JSON.
  3. Java code saves JSON.
  4. Repeat and Java code compiles succesive JSONs for each page.
  5. Java code submits one mega JSON to server.

Another problem I have is with 5. How开发者_StackOverflow中文版 would you go about submitting large JSON data to the server securely ?


On the client-side, if you support only modern browsers, you can use the native JSON.stringify() API. Otherwise, the json2.js library is fine.

On the server-side, there are a herd of libraries to have a look at:

  • Jackson
  • json-lib
  • gson
  • and many more Java JSON libraries...

I'm pointing you to Jackson first at it seems to be the fastest in many cases. However, I find its documentation harder to get my mind around every time I need to get back to it. Json-lib is sometimes easier to get to grasp with for smaller tasks that do not require top-speed, but with still completely acceptable results. Gson as also a good reputation and is very flexible, however the previous benchmarks I came across seemed to indicate that it did not perform as well as Jackson. The newly released 1.5 version might have improved that, but I don't know.

It comes down to the degree of flexibility you want, the performance you need, and whether you want a simple API or if you don't mind a more complex one.

Regarding security, I think your best option here would be to support SSL for the connections. Otherwise you could just make things harder for eavesdroppers by simply using JS-based encryption, but that won't protect you too much. Look for SJCL (Stanford Javascript Crypto Library) for this.


I would recommend you use json2.js. The nice part is that if native JSON is available, you use it with the same interface. json2.js takes care of adding the functions conditionally.

To submit the data, do:

$.ajax({
    type: 'post',
    data: JSON.stringify(data),
    contentType: 'application/json',
    dataType: 'json'
});

This also assumes the server is responding with JSON.

If you want security while in transit, you can use SSL.

There is an existing question about Java JSON parsers. I happen to like json-simple. It lives up to its name, and JSONObject and JSONArray implement Map and List respectively, a feature I find useful.


On the Java side you can use Flexjson to parse the JSON. It's pretty simple to use and performs pretty well compared to other JSON deserializers. Furthermore, it deserialize it directly into your model objects instead of you having to write boiler code to translate between JSONObject/JSONArray into your data model.

http://flexjson.sourceforge.net

As far as your questions about securely, and "Java code submits one MEGA JSON to server" doesn't make sense. What use cases are you worried about?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜