net.sf.json的常用api

1、字符串转JSONObject

String aliBody = (String) memCachedClient.get(signKey);
JSONObject aliBodyObj = JSONObject.fromObject(aliBody);

2、JSONObject 转实体类:  

JSONObject userObject = JSONObject.fromObject(aliBody);
User userVo = (User) JSONObject.toBean(userObject, User.class);

3、将java实体类转JSONObject

User user = new User("aa", "17");

JSONObject jsonObject = JSONObject.fromObject(user);

4、list转数组:

List list = new ArrayList(); 

list.add(new User("aa", "18")); 

list.add(new User("bb", "22")); 

JSONArray jsonArray = JSONArray.fromObject(list);

5、json字符串转list

String jsondata = "[{\"password\":\"18\",\"username\":\"aa\"},{\"age\":\"22\",\"username\":\"bb\"}]";
JSONArray jsonArray = JSONArray.fromObject(jsondata);
List list = new ArrayList();
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
User userVo = (User) JSONObject.toBean(userObject, User.class);
list.add(userVo);
}
//或者直接转
list = Arrays.asList(jsonArray);

 

上一篇:个人如何接入微信支付宝收款?站长付个人收款如何接入?


下一篇:微信小程序珊瑚文本检测(Java案例)