- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
- import java.util.List;
-
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
-
- public class MainClass {
-
- public static void main(String[] args) {
- TestJsonBean();
- TestJsonAttribute();
- TestJsonArray();
- }
-
- @SuppressWarnings("rawtypes")
- private static void TestJsonArray() {
- Student student1 = new Student();
- student1.setId(1);
- student1.setName("jag");
- student1.setSex("man");
- student1.setAge(25);
- student1.setHobby(new String[]{"篮球","游戏"});
-
- Student student2 = new Student();
- student2.setId(2);
- student2.setName("tom");
- student2.setSex("woman");
- student2.setAge(23);
- student2.setHobby(new String[]{"上网","跑步"});
-
- List<Student> list = new ArrayList<Student>();
- list.add(student1);
- list.add(student2);
-
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray.toString());
-
- JSONArray new_jsonArray=JSONArray.fromObject(jsonArray.toArray());
- Collection java_collection=JSONArray.toCollection(new_jsonArray);
- if(java_collection!=null && !java_collection.isEmpty())
- {
- Iterator it=java_collection.iterator();
- while(it.hasNext())
- {
- JSONObject jsonObj=JSONObject.fromObject(it.next());
- Student stu=(Student) JSONObject.toBean(jsonObj,Student.class);
- System.out.println(stu.getName());
- }
- }
- }
-
- private static void TestJsonAttribute() {
-
- JSONObject jsonObj = new JSONObject();
- jsonObj.put("Int_att",25);
- jsonObj.put("String_att","str");
- jsonObj.put("Double_att",12.25);
- jsonObj.put("Boolean_att",true);
-
- JSONObject jsonObjSon = new JSONObject();
- jsonObjSon.put("id", 1);
- jsonObjSon.put("name", "tom");
- jsonObj.put("JSONObject_att",jsonObjSon);
-
- JSONArray jsonArray = new JSONArray();
- jsonArray.add("array0");
- jsonArray.add("array1");
- jsonArray.add("array2");
- jsonArray.add("array3");
- jsonObj.put("JSONArray_att", jsonArray);
- System.out.println(jsonObj.toString());
- System.out.println("Int_att:"+jsonObj.getInt("Int_att"));
- System.out.println("String_att:"+jsonObj.getString("String_att"));
- System.out.println("Double_att:"+jsonObj.getDouble("Double_att"));
- System.out.println("Boolean_att:"+jsonObj.getBoolean("Boolean_att"));
- System.out.println("JSONObject_att:"+jsonObj.getJSONObject("JSONObject_att"));
- System.out.println("JSONArray_att:"+jsonObj.getJSONArray("JSONArray_att"));
- }
-
-
- private static void TestJsonBean() {
-
- Student student = new Student();
- student.setId(1);
- student.setName("jag");
- student.setSex("man");
- student.setAge(25);
- student.setHobby(new String[]{"篮球","上网","跑步","游戏"});
-
- JSONObject jsonStu = JSONObject.fromObject(student);
- System.out.println(jsonStu.toString());
- System.out.println(jsonStu.getJSONArray("hobby"));
-
- Student stu = (Student) JSONObject.toBean(jsonStu, Student.class);
- System.out.println(stu.getName());
-
- JSONObject jsonObj = new JSONObject();
- jsonObj.put("id",1);
- jsonObj.put("name","张勇");
- jsonObj.put("sex","男");
- jsonObj.put("age",24);
-
-
-
- Student stud = (Student) JSONObject.toBean(jsonObj,Student.class);
- System.out.println(stud.getName());
- }
- }
posted @
2015-08-28 16:07
郑文亮
阅读(
1396)
评论()
编辑
收藏
举报
点击右上角即可分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
2013-08-28 ajax弹出窗口
2012-08-28 mysql远程连接对用户的测试(吴龙波)
2012-08-28 MySQL 显示表字段及注释等信息
2012-08-28 Java连接mysql数据库