本文实例讲述了php array 转json及java 转换 json数据格式操作。分享给大家供大家参考,具体如下:
php array 转json白狗秋千架 数据
$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”]=>
st路由器传输速度ring(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,
添加至构建路径。
package main;import net.sf.json.jsonobject;import net.sf.json.json板桥体array;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.tname("谭勇"); demo1.tage(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
package main;public class demo1 { private string name; private int age; public void tname(string name) { this.name = name; } public string getname() { return this.name; } public void tage(int age) { this.age = age; } public int getage() { return this.age; }}
本文发布于:2023-04-08 05:48:27,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/dc862a5f964a1c13e26eec8e0c231506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php array 转json及java 转换 json数据格式操作示例.doc
本文 PDF 下载地址:php array 转json及java 转换 json数据格式操作示例.pdf
留言与评论(共有 0 条评论) |