Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON Data Formatting
    text
    copied!<p>I am passing the following JSON object from a .jsp page to a java servlet using JSON.stringify and JQuery.ajax():</p> <pre><code>{"bin":[{"binId":"0","binDetails":[{"productCode":"AU192","qty":"4"},{"productCode":"NE823","qty":"8"}],"comments":"store pickup"},{"binId":"1","binDetails":[{"productCode":"AF634","qty":"2"}],"comments":""},{"binId":"2","binDetails":[{"productCode":"QB187","qty":"3"}],"comments":"international shipping"},{"binId":"3","binDetails":[{"productCode":"AF634","qty":"2"},{"productCode":"QB187","qty":"2"}],"comments":""}]} </code></pre> <p>This is the code in my java servlet:</p> <pre><code>StringBuffer strBuffer = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while((line = reader.readLine()) != null){ strBuffer.append(line); } } catch (Exception e) { } try { JSONObject jsonObj = new JSONObject(new JSONTokener(strBuffer.toString())); // I call a method here and pass jsonObj } catch (Exception e) { } </code></pre> <p>In the method to which I pass jsonObj I am using jsonObj.length() to find out how many items are in jsonObj and it tells me 1, which in this case I would have expected 3. I even tried this:</p> <pre><code>JSONObject bins = jsonObj.get("bin"); bins.length(); </code></pre> <p>which told me jsonObj.get("bin") was not a JSONObject. Is my data formatted incorrectly before I pass it from my .jsp or am I using the JSONObject in my java servlet incorrectly? How do I access the values in the JSONObject?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload