php array 转json及java 转换 json数据格式操作示例

这篇文章主要介绍了php array 转json及java 转换 json数据格式操作,结合实例形式分析了PHP针对array数组转json以及Java操作Map、List、对象与json格式转换的相关实现技巧,需要的朋友可以参考下。

本文实例讲述了php array 转json及java 转换 json数据格式操作,分享给大家供大家参考,具体如下:

php array 转json 数据

  1. $arr = array(
  2. "demo1" => "my demo1",
  3. "demo2" => array(
  4. "demo2_demo1"=>"aaaaaaaa",
  5. "demo2_demo2"=>"bbbbbbbb",
  6. "demo2_demo3"=>array(
  7. "demo2_demo3_demo1"=>"ccccccc"
  8. )
  9. ),
  10. "demo3" => 22
  11. );
  12. $json_str = json_encode($arr,true);
  13. $arr = json_decode($json_str,true);
  14. var_dump($json_str);
  15. var_dump($arr);

查看结果

运行结果:

  1. string(137) "{"demo1":"my demo1","demo2":{"demo2_demo1":"aaaaaaaa","demo2_demo2":"bbbbbbbb","demo2_demo3":{"demo2_demo3_demo1":"ccccccc"}},"demo3":22}"
  2. array(3) {
  3. ["demo1"]=>
  4. string(8) "my demo1"
  5. ["demo2"]=>
  6. array(3) {
  7. ["demo2_demo1"]=>
  8. string(8) "aaaaaaaa"
  9. ["demo2_demo2"]=>
  10. string(8) "bbbbbbbb"
  11. ["demo2_demo3"]=>
  12. array(1) {
  13. ["demo2_demo3_demo1"]=>
  14. string(7) "ccccccc"
  15. }
  16. }
  17. ["demo3"]=>
  18. int(22)
  19. }

java json数据格式转换依赖包

commons-beanutils-1.8.3.jar,

commons-collections-3.2.1.jar,

commons-lang-2.6.jar,

commons-logging-1.1.1.jar,

ezmorph-1.0.6.jar,

json-lib-2.4-jdk15.jar,

添加至构建路径。

  1. package Main;
  2. import net.sf.json.JSONObject;
  3. import net.sf.json.JSONArray;
  4. import java.util.Map;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.ArrayList;
  8. import Main.Demo1;
  9. public class Index
  10. {
  11. public static void main(String[] args)
  12. {
  13. index4();
  14. }
  15. /**
  16. * 简单的simple
  17. * */
  18. public static void index1()
  19. {
  20. JSONObject json = new JSONObject();
  21. json.element("name","谭勇");
  22. json.element("age",22);
  23. System.out.println(json.toString());
  24. }
  25. /**
  26. * Map 数据转json
  27. * */
  28. public static void index2()
  29. {
  30. JSONObject json = new JSONObject();
  31. Map<String,String> map = new HashMap<String,String>();
  32. map.put("name", "谭勇");
  33. map.put("age", "22");
  34. json.accumulateAll(map);
  35. System.out.println(json.toString());
  36. }
  37. /**
  38. * List<Map> 转json
  39. * */
  40. public static void index3()
  41. {
  42. JSONArray arr = new JSONArray();
  43. List<Map<String,String>> list = new ArrayList<Map<String,String>>();
  44. list.add(getMap("name","谭勇"));
  45. list.add(getMap("age","22"));
  46. arr.addAll(list);
  47. System.out.println(arr.toString());
  48. }
  49. /**
  50. * 对象转Json
  51. * */
  52. public static void index4()
  53. {
  54. Demo1 demo1 = new Demo1();
  55. demo1.setName("谭勇");
  56. demo1.setAge(22);
  57. JSONObject json = new JSONObject();
  58. JSONArray arr = new JSONArray();
  59. json.element("demo1",demo1);
  60. arr.add(demo1);
  61. //json.containsKey(demo1);
  62. System.out.println(json.toString());
  63. System.out.println(arr.toString());
  64. }
  65. private static Map<String,String> getMap(String key,String val)
  66. {
  67. Map<String,String> map = new HashMap<String,String>();
  68. map.put(key, val);
  69. return map;
  70. }
  71. }

Demo1.java

  1. package Main;
  2. public class Demo1
  3. {
  4. private String name;
  5. private int age;
  6. public void setName(String name)
  7. {
  8. this.name = name;
  9. }
  10. public String getName()
  11. {
  12. return this.name;
  13. }
  14. public void setAge(int age)
  15. {
  16. this.age = age;
  17. }
  18. public int getAge()
  19. {
  20. return this.age;
  21. }
  22. }

更多关于 JSONObject 提供的方法 JSONArray 提供的方法 自己探索。