版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dalaijianfen/article/details/44220517
得到js中用ajax传过来的数据 {jsonDatas:{'updateda':[{"customerName":"123","id":2,"contactWay":"13104712589","linkman":"123"},{ }]}}
方法一:
JSONObject JsonObject = JSONObject.fromObject(jsonDatas);
JSONArray jsonAry = (JSONArray) JsonObject.get("updateda");
List<Customer> editList = JSONArray.toList(jsonAry,Customer.class);//重点
直接从前台的标准的json,然后变为jsonobject,如果里面是key:value的形式的话,可以直接用getString(key)直接得到value
当为list的时候,转为jsonarray然后利用tolist方法变为list结构,然后进行遍历操作
方法二:
JSONObject JsonObject = JSONObject.fromObject(jsonDatas);
JSONArray jsonAry = (JSONArray) JsonObject.get("updateda");
for (int i = 0; i < jsonAry.size(); i++) {
JSONObject o = jsonAry.optJSONObject(i);//*****重点
Customer customer = new Customer();
customer.setId(o.getInt("id"));
//Customer customer = cd.getByID(Customer.class, o.getInt("id"));
customer.setCustomerName(o.getString("customerName"));
customer.setLinkman(o.getString("linkman"));
customer.setContactWay(o.getString("contactWay"));
System.out
.println(customer.getId() + "****"
+ customer.getCustomerName() + "****"
+ customer.getLinkman() + "****"
+ customer.getContactWay());
editList.add(customer);
}
同理得到jsonarray,对jsonarray进行一次遍历,并且利用jsonAry.get("updateda");的方式将list变为object,然后声明对象进行赋值