0

JSON.stringify と JQuery.ajax() を使用して、次の JSON オブジェクトを .jsp ページから Java サーブレットに渡しています。

{"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":""}]}

これは私のJavaサーブレットのコードです:

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) {
}

jsonObj を渡すメソッドでは、jsonObj.length() を使用して jsonObj に含まれるアイテムの数を調べていますが、1 と表示されます。この場合は 3 と予想されます。

JSONObject bins = jsonObj.get("bin");
bins.length();

jsonObj.get("bin") は JSONObject ではないことがわかりました。.jsp からデータを渡す前に、データの形式が正しくありませんか、それとも Java サーブレットで JSONObject を正しく使用していませんか? JSONObject の値にアクセスするにはどうすればよいですか?

4

1 に答える 1

2
JSONArray bins = jsonObj.getJSONArray("bin");
bins.length();
于 2012-05-03T20:54:15.227 に答える