JSON.stringify (array) from jsp to servlet (loop using javax.json-api1.0. Jar or javax.json-1.0.jar)
HTML:
<table>
<tr>
<td>
<input type="hidden" value="flag1" />
</td>
<td>
<input type="text" value="orange" />
</td>
<td>
<input type="text" value="1.00" />
</td>
<td>
<input type="text" value="5" />
</td>
</tr>
<tr>
<td>
<input type="hidden" value="flag2" />
</td>
<td>
<input type="text" value="apple" />
</td>
<td>
<input type="text" value="2.00" />
</td>
<td>
<input type="text" value="5" />
</td>
</tr>
</table>
JS:
var array = $.map($('table tr'), function (val, i) {
var obj = {}, inputs = $(val).find('td input:not(:hidden)');
obj[inputs.filter(':first').val()] = $.map(inputs.not(':first'), function (val, i) {
return val.value;
});
return obj;
});
alert(JSON.stringify(array));
$(document).on("click","#save",function(){
$.post("servlet.html","data="+JSON.stringify(array)+"",function(response){
});
});
Java servlet contains the following:
String data = request.getParameter("data");
the data looks like this:
[{"flag1":["orange","1.00","5"]},{"flag2":["apple","2.00","5"]}]//this is get from table row data using stringify
I want to get into the first loop using only javax.json-api1.0.jar
or javax.json-1.0.jar
:
in the first loop:
flag1
Orange
1.00
5
in the second loop:
flag2
Apple
2.00
5
Any help would be better.
source to share
You can only do this with json-api, but you must be sure about the structure of the input. If not, gson or jackson2 is easier to use.
If you want to use json-api you can do something like:
First declare the attribute JsonFactoryReader
in your servlet because you will create a reader for each request.
JsonReaderFactory readerFactory = Json.createReaderFactory(null);
then in your method service
or doXXX
do:
String data = request.getParameter("data");
// create a reader for json data
JsonReader jr = readerFactory.createReader(new StringReader(data));
// tol level must be an array, and we count the iteration for eventual error messages
JsonArray ja = jr.readArray();
int iteration = 0;
for (JsonValue jv: ja) {
// sub elements must be dicts
if (jv.getValueType() != ValueType.OBJECT) {
throw new FormatException(iteration, jv);
}
for (Entry<String, JsonValue> e: ((JsonObject) jv).entrySet()) {
// the key
String key = e.getKey();
if (e.getValue().getValueType() != ValueType.ARRAY) {
throw new FormatException(iteration, e.getValue());
}
// the values, first is a string
JsonArray array = (JsonArray) e.getValue();
for (int i=0; i<array.size(); i++) {
System.out.println(array.get(0).getValueType());
}
String name = array.getString(0);
// next a float and an int
float fval;
int ival;
try {
fval = Float.valueOf(array.getString(1));
ival = Integer.valueOf(array.getString(2));
}
catch (NumberFormatException ex) {
throw new FormatException(iteration, array);
}
// Do your stuff with those values ...
// for instance Data data = new Data(key, name, fval, ival) ...
iteration++;
}
}
I suggest you an exception class for handling format errors (at least you know why it crashed ...):
class FormatException extends ServletException {
FormatException(int i, JsonValue jv) {
super("Iteration " + String.valueOf(i) + " : " + jv.toString());
}
}
source to share