Reputation: 341
To illustrate my point: I have Name Age and Address of 4 people
[Sam, 23, nj],
[Nome, 25, ny],
[Sim, 20, pa],
[Jack, 12, pa]
I need to send these 4 rows to the java side.
I am familiar with java side coding so I can retrieve 4 rows from a list etc etc. I am new to javascripts, so I was wondering how to put all this data into a variable and then send it to java, so that java understands i am sending 4 sets of data.
I wanted to do hashmap of objects, either way as I am a newbie i don't know which road to take.pls advice.
I know how to post data through ajax, only part i am struggling with is creating the arrAY so my java side can parse it.
Upvotes: 1
Views: 2433
Reputation:
Java supports JSON objects: http://www.json.org/java/ Use these methods.
Convert the array format to a stored variable and use the stringify method.
var a = Array([Sam, 23, nj], [Nome, 25, ny], [Sim, 20, pa], [Jack, 12, pa]);
var jsonText = JSON.stringify(a);
//send jsonText to java via post after this
Edit
To answer below, my Java is rusty from disuse but you will need to use the libraries here https://github.com/douglascrockford/JSON-java, and send the client side data jsonText over post data:
<form method='post' action='/Source/jsp/X.jsp' id='jsonform'>
<input id='json' name='json' value=''>
</form>
Then run a script to fill the value of the input with jsonText and submit it.
Once that is done you can grab the POST data and deserialize it and use it in your Java program.
var input = document.getElementById('json');
var form = document.getElementById('jsonform');
input.value = jsonText;
form.submit();
For the Java part, you said you are familiar with it so I'll leave that bit to you.
Upvotes: 1
Reputation: 134167
You could serialize it to JSON so it looks something like this:
[{name: "Sam", age: 23, address: "nj"},
{name: "Nome", age: 25, address: "ny"}]
Upvotes: 1