我的ajax传递了一个json数组,如下所示:
{"formData":[{"cusID":"2"},{"empID":"1"}],"invoice":578416969}我正在尝试使用javax.json库获取数据。
JSONObject jsonObj = new JSONObject(jasonString);我能够获取invoice的值
Integer invoiceNum = (Integer) jsonObj.get("invoice");但是,通过执行以下操作,我无法获取cusID和empID的值:
Integer cusId = Integer.parseInt((String) jsonObj.get("cusID"));
Integer empId = Integer.parseInt((String) jsonObj.get("empID"));错误message:org.json.JSONException: JSONObject"cusID“未找到。
我做错什么了?我愿意接受一些建议,如果您有更好的方法处理这个json数据,我愿意使用它。
发布于 2015-10-12 18:52:16
cusID实际上是数组formData中第一个对象的属性。
jsonObj.getJsonArray("formData").getJsonObject(0).get("cusID");应该能起作用。
发布于 2015-10-12 18:52:42
首先必须将formData作为数组,然后获取第一个元素和custId,然后获取第二个元素和empID。
发布于 2015-10-14 15:42:41
您可以使用Gson()库。(com.google.gson.Gson)它使你变得简单。
JsonArray formData = jsonElement.getAsJsonObject().get("formData").getAsJsonArray();
Integer cusId = formData.get(0).getAsJsonObject().get("cusID").getAsInt();
Integer empId = formData.get(1).getAsJsonObject().get("empID").getAsInt();https://stackoverflow.com/questions/33088164
复制相似问题