org.json的用法

前端之家收集整理的这篇文章主要介绍了org.json的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

闲话少说,通过实例简单介绍一下org.json的用法,用到的jar包是json-20090211.jar

  1. <pre name="code" class="java">package com.ilucky.util.orgjson;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.json.JSONArray;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11.  
  12. /**
  13. * 重点:JSONObject对应集合Map,JSONArray对应集合List.
  14. * 用json类(fastjson和orgjson)库处理复杂的集合时,要将集合直接放到value处,不能将其转换为json字符串放到value处,* 如果将其转换为字符串放到value处,最终生成的json字符串会带有转义字符.
  15. * @author IluckySi
  16. * @since 20150407
  17. */
  18. public class OrgjsonUtil {
  19.  
  20. public static void main(String[] args) {
  21. try {
  22. //JSONObject的用法.
  23. System.out.println("-----test1-----");
  24. testJsonObject();
  25. //JSONArray的用法.
  26. System.out.println("-----test2-----");
  27. testJsonArray();
  28. //JSONObject,JSONArray和集合的复杂用法.
  29. System.out.println("-----test3-----");
  30. testComplex();
  31. //JSONObject,JSONArray和集合的复杂用法.
  32. System.out.println("-----test4-----");
  33. testComplex2();
  34. } catch (Exception e) {
  35. System.out.println("测试发生异常: " + e);
  36. }
  37. }
  38. public static void testJsonObject() throws JSONException {
  39. JSONObject jo = new JSONObject();
  40. jo.put("username","IluckySi");
  41. jo.put("age",27);
  42. jo.put("sex",true);
  43. Map<String,String> skill = new HashMap<String,String>();
  44. skill.put("java","不错");
  45. skill.put("javascript","凑合");
  46. skill.put("jquery","挺好");
  47. jo.put("skill",skill);
  48. String username = jo.getString("username");
  49. int age = jo.getInt("age");
  50. boolean sex = jo.getBoolean("sex");
  51. JSONObject skill2 = new JSONObject(jo.getString("skill"));
  52. System.out.println(username + ",年龄 = " + age + ",性别 = " + (sex == true ? "男" : "女") + ",技能如下:");
  53. String[] names = JSONObject.getNames(skill2);
  54. for(String name : names) {
  55. System.out.println(name + ": " + skill2.getString(name));
  56. }
  57. }
  58. public static void testJsonArray() throws JSONException {
  59. JSONArray ja = new JSONArray();
  60. JSONObject jo = new JSONObject();
  61. jo.put("username",true);
  62. ja.put(jo);
  63. JSONObject jo2 = new JSONObject();
  64. jo2.put("username","IluckySi2");
  65. jo2.put("age",28);
  66. jo2.put("sex",false);
  67. ja.put(jo2);
  68. for(int i = 0; i < ja.length(); i++) {
  69. JSONObject j = (JSONObject)ja.get(i);
  70. String username = j.getString("username");
  71. int age = j.getInt("age");
  72. boolean sex = j.getBoolean("sex");
  73. System.out.println(username + ",性别 = " + (sex == true ? "男" : "女"));
  74. }
  75. }
  76. public static void testComplex() throws JSONException {
  77. JSONObject jo = new JSONObject();
  78. jo.put("result","success");
  79. List<Map<Object,Object>> list = new ArrayList<Map<Object,Object>>();
  80. Map<Object,Object> user1 = new HashMap<Object,Object>();
  81. user1.put("name","name1");
  82. user1.put("password","password1");
  83. list.add(user1);
  84. Map<Object,Object> user2 = new HashMap<Object,Object>();
  85. user2.put("name","name2");
  86. user2.put("password","password2");
  87. list.add(user2);
  88. jo.put("message",list);
  89. String send = jo.toString();
  90. System.out.println("要测试的消息" + send);
  91. send(send);
  92. }
  93. private static void send(String send) throws JSONException {
  94. JSONObject jo = new JSONObject(send);
  95. String result = jo.getString("result");
  96. System.out.println("result=" + result);
  97. JSONArray ja = new JSONArray(jo.getString("message"));
  98. for(int i = 0; i < ja.length(); i++) {
  99. JSONObject j = (JSONObject)ja.get(i);
  100. String name = j.getString("name");
  101. String password = j.getString("password");
  102. System.out.println("name = " + name + ",password = " + password);
  103. }
  104. }
  105. public static void testComplex2() throws JSONException {
  106. JSONObject jo = new JSONObject();
  107. jo.put("result","success");
  108. JSONObject message = new JSONObject();
  109. List<Map<Object,"password2");
  110. list.add(user2);
  111. message.put("message",list);
  112. jo.put("message",message.toString());//注意:如果直接写message,也不会有转义字符.
  113. String send = jo.toString();
  114. System.out.println("要测试的消息" + send);
  115. send2(send);
  116. }
  117. private static void send2(String send) throws JSONException {
  118. JSONObject jo = new JSONObject(send);
  119. String result = jo.getString("result");
  120. System.out.println("result=" + result);
  121. JSONObject message = new JSONObject(jo.getString("message"));
  122. JSONArray message2 = new JSONArray(message.getString("message"));
  123. for(int i = 0; i < message2.length(); i++) {
  124. JSONObject j = (JSONObject)message2.get(i);
  125. String name = j.getString("name");
  126. String password = j.getString("password");
  127. System.out.println("name = " + name + ",password = " + password);
  128. }
  129. }
  130. }
  131.  
  132. /**
  133. 输出结果:
  134. -----test1-----
  135. IluckySi,年龄 = 27,性别 = 男,技能如下:
  136. javascript: 凑合
  137. java: 不错
  138. jquery: 挺好
  139. -----test2-----
  140. IluckySi,性别 = 男
  141. IluckySi2,年龄 = 28,性别 = 女
  142. -----test3-----
  143. 要测试的消息{"message":[{"name":"name1","password":"password1"},{"name":"name2","password":"password2"}],"result":"success"}
  144. result=success
  145. name = name1,password = password1
  146. name = name2,password = password2
  147. -----test4-----
  148. 要测试的消息{"message":"{\"message\":[{\"name\":\"name1\",\"password\":\"password1\"},{\"name\":\"name2\",\"password\":\"password2\"}]}",password = password2
  149.  
  150. */

猜你在找的Json相关文章