JSON字符串转换为对象

约定不等于承诺〃 2022-06-08 00:28 426阅读 0赞

首先理解JSONObject 和JSONArray,JSONObject就代表{“key”:”value”,”key”:”value”}格式,不管多么复杂都是这种格式,JSONArray代表[{“key”:”value”},{“key”:”value”}]格式,原理一样,不管多么复杂,都是这种格式。



net.sf.json-lib

json-lib

2.4

jdk15

下面例子展示了怎么把对象转换成字符串,把复杂字符串解析成对象

  1. package JSON.JSONObjectAndJSONArray;
  2. import org.junit.Before;
  3. import JSON.entity.Student;
  4. import junit.framework.Test;
  5. import junit.framework.TestCase;
  6. import junit.framework.TestSuite;
  7. import net.sf.json.JSONArray;
  8. import net.sf.json.JSONObject;
  9. /**
  10. * Unit test for simple App.
  11. */
  12. public class AppTest extends TestCase {
  13. /**
  14. * Create the test case
  15. *
  16. * @param testName
  17. * name of the test case
  18. */
  19. public AppTest(String testName) {
  20. super(testName);
  21. }
  22. /**
  23. * @return the suite of tests being tested
  24. */
  25. public static Test suite() {
  26. return new TestSuite(AppTest.class);
  27. }
  28. /**
  29. * Rigourous Test :-)
  30. */
  31. @org.junit.Test
  32. public void testApp() {
  33. Student stu1 = new Student("001", "小红", "24");
  34. Student stu2 = new Student("002", "小黄", "25");
  35. Student stu3 = new Student("003", "小绿", "23");
  36. JSONObject jsonObject = new JSONObject();
  37. JSONArray jsonArray = new JSONArray();
  38. jsonObject.put("stu1", stu1);
  39. jsonObject.put("stu2", stu2);
  40. jsonObject.put("stu3", stu3);
  41. String jsonString = jsonObject.toString();
  42. System.out.println(jsonString);
  43. // {"stu1":{"age":"24","id":"001","name":"小红"},"stu2":{"age":"25","id":"002","name":"小黄"},"stu3":{"age":"23","id":"003","name":"小绿"}}
  44. jsonArray.add(stu1);
  45. jsonArray.add(stu2);
  46. jsonArray.add(stu3);
  47. System.out.println(jsonArray.toString());
  48. // [{"age":"24","id":"001","name":"小红"},{"age":"25","id":"002","name":"小黄"},{"age":"23","id":"003","name":"小绿"}]
  49. jsonObject = new JSONObject();
  50. jsonObject.put("student", jsonArray);
  51. System.out.println(jsonObject);
  52. // {"student":[{"age":"24","id":"001","name":"小红"},{"age":"25","id":"002","name":"小黄"},{"age":"23","id":"003","name":"小绿"}]}
  53. System.out.println("----------------");
  54. String str = "{\"stu1\":{\"age\":\"24\",\"id\":\"001\",\"name\":\"小红\"},\"stu2\":{\"age\":\"25\",\"id\":\"002\",\"name\":\"小黄\"},\"stu3\":{\"age\":\"23\",\"id\":\"003\",\"name\":\"小绿\"}}";
  55. JSONObject jsonObject2 = JSONObject.fromObject(str);
  56. Object object = jsonObject2.get("stu1");
  57. System.out.println(object);
  58. // {"age":"24","id":"001","name":"小红"}
  59. JSONObject jsonBean = JSONObject.fromObject(object);
  60. System.out.println("jsonBean---" + jsonBean);
  61. // jsonBean---{"age":"24","id":"001","name":"小红"}
  62. Object bean = JSONObject.toBean(jsonBean, Student.class);
  63. if (bean instanceof Student) {
  64. System.out.println(bean);
  65. // Student [id=001, name=小红, age=24]
  66. }
  67. System.out.println("--------------");
  68. String str2 = "{\"student\":[{\"age\":\"24\",\"id\":\"001\",\"name\":\"小红\"},{\"age\":\"25\",\"id\":\"002\",\"name\":\"小黄\"},{\"age\":\"23\",\"id\":\"003\",\"name\":\"小绿\"}]}";
  69. JSONObject jsonObject3 = JSONObject.fromObject(str2);
  70. Object object2 = jsonObject.get("student");
  71. JSONArray jsonArray2 = JSONArray.fromObject(object2);
  72. for (int i = 0; i < jsonArray2.size(); i++) {
  73. Object object3 = jsonArray2.get(i);
  74. JSONObject object4 = jsonObject.fromObject(object3);
  75. Object bean2 = JSONObject.toBean(object4, Student.class);
  76. if (bean2 instanceof Student) {
  77. System.out.println(bean2);
  78. /*
  79. * Student [id=001, name=小红, age=24]
  80. * Student [id=002, name=小黄, age=25]
  81. * Student [id=003, name=小绿, age=23]
  82. */
  83. }
  84. }
  85. }
  86. }

Center

发表评论

表情:
评论列表 (有 0 条评论,426人围观)

还没有评论,来说两句吧...

相关阅读