php array 转json及java 转换 json数据格式操作示例
本文实例讲述了php array 转json及java 转换 json数据格式操作。分享给大家供大家参考,具体如下:
php array 转json 数据
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $arr = array ( "demo1" => "my demo1" , "demo2" => array ( "demo2_demo1" => "aaaaaaaa" , "demo2_demo2" => "bbbbbbbb" , "demo2_demo3" => array ( "demo2_demo3_demo1" => "ccccccc" ) ), "demo3" => 22 ); $json_str = json_encode( $arr ,true); $arr = json_decode( $json_str ,true); var_dump( $json_str ); var_dump( $arr ); //查看结果 |
运行结果:
string(137) "{"demo1":"my demo1","demo2":{"demo2_demo1":"aaaaaaaa","demo2_demo2":"bbbbbbbb","demo2_demo3":{"demo2_demo3_demo1":"ccccccc"}},"demo3":22}"
array(3) {
["demo1"]=>
string(8) "my demo1"
["demo2"]=>
array(3) {
["demo2_demo1"]=>
string(8) "aaaaaaaa"
["demo2_demo2"]=>
string(8) "bbbbbbbb"
["demo2_demo3"]=>
array(1) {
["demo2_demo3_demo1"]=>
string(7) "ccccccc"
}
}
["demo3"]=>
int(22)
}
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package Main; import net.sf.json.JSONObject; import net.sf.json.JSONArray; import java.util.Map; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import Main.Demo1; public class Index { public static void main(String[] args) { index4(); } /** * 简单的simple * */ public static void index1() { JSONObject json = new JSONObject(); json.element( "name" , "谭勇" ); json.element( "age" , 22 ); System.out.println(json.toString()); } /** * Map 数据转json * */ public static void index2() { JSONObject json = new JSONObject(); Map<String,String> map = new HashMap<String,String>(); map.put( "name" , "谭勇" ); map.put( "age" , "22" ); json.accumulateAll(map); System.out.println(json.toString()); } /** * List<Map> 转json * */ public static void index3() { JSONArray arr = new JSONArray(); List<Map<String,String>> list = new ArrayList<Map<String,String>>(); list.add(getMap( "name" , "谭勇" )); list.add(getMap( "age" , "22" )); arr.addAll(list); System.out.println(arr.toString()); } /** * 对象转Json * */ public static void index4() { Demo1 demo1 = new Demo1(); demo1.setName( "谭勇" ); demo1.setAge( 22 ); JSONObject json = new JSONObject(); JSONArray arr = new JSONArray(); json.element( "demo1" ,demo1); arr.add(demo1); //json.containsKey(demo1); System.out.println(json.toString()); System.out.println(arr.toString()); } private static Map<String,String> getMap(String key,String val) { Map<String,String> map = new HashMap<String,String>(); map.put(key, val); return map; } } |
Demo1.java
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package Main; public class Demo1 { private String name; private int age; public void setName(String name) { this .name = name; } public String getName() { return this .name; } public void setAge( int age) { this .age = age; } public int getAge() { return this .age; } } |
更多关于 JSONObject 提供的方法 JSONArray 提供的方法 自己探索。
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/u014559227/article/details/73771373
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。