3

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.

4
  • 1
    please show us what you have tired for serverside code to manipulate the json string. Commented Aug 4, 2014 at 3:38
  • @sunleo my code only in java servlet is ` String data = request.getParameter("data");` . havent tried anything using those libraries. Im new to json so classes on that is very new to me.sorry. Commented Aug 4, 2014 at 3:40
  • 1
    please check this link programmingforliving.com/2013/07/… Commented Aug 4, 2014 at 3:52
  • take a look at the apidocs: docs.oracle.com/javaee/7/api/javax/json/JsonReader.html Commented Aug 6, 2014 at 7:39

1 Answer 1

1

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.