bumbumpaw
bumbumpaw

Reputation: 2528

JSON.stringify(array) from jsp to servlet (loop using javax.json-api1.0. jar or javax.json-1.0.jar only)

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 this :

 String data = request.getParameter("data");

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 on first loop using javax.json-api1.0.jar or javax.json-1.0.jar only :

on first loop :

 flag1
 Orange
 1.00
 5

on second loop :

flag2
Apple
2.00
5

Any help will be best.

Upvotes: 3

Views: 1733

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148965

You can do it with only json-api, but you must be sure of the structure of the input data. If not, gson or jackson2 are easier to use.

If you want to use json-api, you could do something like :

First declare a JsonFactoryReader attribute in your servlet because you will create a reader per request.

JsonReaderFactory readerFactory = Json.createReaderFactory(null);

then in your service or doXXX method 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 propose you an exception class to handle format errors (at least you know why it breaked ...):

class FormatException extends ServletException {
    FormatException(int i, JsonValue jv) {
        super("Iteration " + String.valueOf(i) + " : " + jv.toString());
    }
}

Upvotes: 1

Related Questions