org.json的使用

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

jar包的下载到mvn里面去下载好了http://mvnrepository.com/artifact/org.json/json/20170516

直接上代码分析代码最好不过了?自己拿去运行分析一下就是你自己的东西了。


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

猜你在找的Json相关文章