"Deep Java" converts a JSON string into map/list

Source: Internet
Author: User

In the process of reading any questions, welcome to communicate together

Email: [email protected]    

QQ:1494713801

Java code
  1. Package  Jsontomap;
  2. Import  java.util.List;
  3. Import  Java.util.Map;
  4. Import  Java.util.Map.Entry;
  5. Import  Net.sf.json.JSONArray;
  6. Import  Net.sf.json.JSONObject;
  7. /**
  8. * Description JSON string converted to Map/list
  9. * @author XSS
  10. * @date 2013-1-18 10:22:41
  11. * @mail [email protected]
  12.  */   
  13. Public class jsontomap {
  14. Public static void main (string[] args) {
  15. //jsonarray   
  16. String jsonarraydata="[{\" a1\ ": \" 12\ ", \" b1\ ": \" 112\ ", \" c1\ ": \" 132\ ", \" d1\ ": \" 134\ "},{\" a2\ ": \" 12\ ", \" b2\ ": \  "112\", \ "c2\": \ "132\", \ "d2\": \ "134\"},{\ "a3\": \ "12\", \ "b3\": \ "112\", \ "c3\": \ "132\", \ "d3\": \ "134\"}] ";
  17. Jsonarray Jsonarray = Jsonarray.fromobject (Jsonarraydata);
  18. List<map<string,object>> Maplistjson = (List) Jsonarray;
  19. for (int i = 0; i < maplistjson.size (); i++) {
  20. Map<string,object> Obj=maplistjson.get (i);
  21. for (entry<string,object> entry:obj.entrySet ()) {
  22. String strkey1 = Entry.getkey ();
  23. Object Strval1 = Entry.getvalue ();
  24. System.out.println ("KEY:"+strkey1+"-Value:"+strval1+"\ n"   );
  25. }
  26. }
  27. //Jsonobject   
  28. String jsonobjectdata="{\" data1\ ": {\" a1\ ": \" 12\ ", \" b1\ ": \" 112\ ", \" c1\ ": \" 132\ ", \" d1\ ": \" 134\ "},\" data2\ ": {\ "a2\": \ "12\", \ "b2\": \ "112\", \ "c2\": \ "132\", "d2\": \ "134\"},\ "data3\": {\ "a3\": \ "12\", \ "b3\": \ "112\", \ "c3\": \ "  132\ ", \" d3\ ": \" 134\ "}}";
  29. Jsonobject jsonobject = Jsonobject.fromobject (Jsonobjectdata);
  30. map<string, object> Mapjson = Jsonobject.fromobject (Jsonobject);
  31. for (entry<string,object> entry:mapJson.entrySet ()) {
  32. Object Strval1 = Entry.getvalue ();
  33. Jsonobject JsonObjectStrval1 = Jsonobject.fromobject (STRVAL1);
  34. map<string, object> mapJsonObjectStrval1 = Jsonobject.fromobject (JSONOBJECTSTRVAL1);
  35. System.out.println ("KEY:"+entry.getkey () +"-Value:"+entry.getvalue () + "\ n"  );
  36. for (entry<string, object> entry1:mapJsonObjectStrval1.entrySet ()) {
  37. System.out.println ("KEY:"+entry1.getkey () +"-Value:"+entry1.getvalue () +   "\ n");
  38. }
  39. }
  40. }
  41. }

Reference Link: http://wuniu2010.iteye.com/blog/1771934

"Deep Java" converts a JSON string into map/list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.