本文实例讲述了php array 转json及java 转换,json格式

作者:美狮美高梅官方网站

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

本文实例讲述了PHP数组对象与Json转换操作。分享给大家供大家参考,具体如下:

本文实例讲述了php使用json-schema模块实现json校验。分享给大家供大家参考,具体如下:

php array 转json 数据

代码

客户端和服务端的http信息传递,采用json几乎成了标配。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 = json_decode;var_dump;var_dump;//查看结果
$value){ $array[$key] = object_array; } } return $array;}//json到数组function simple_json_parser{ $json = str_replace("{","",str_replace; $jsonValue = explode; $arr = array(); foreach{ $jValue = explode; $arr[str_replace] = (str_replace; } return $arr;}echo "
-------------------------------
";echo "现在的数组

";$arr=array( "name"=>"李小龙", "tel"=>"11111111111", "age"=>"12",);var_dump;echo "
-------------------------------
";echo "

数组转对象

";$obj= arrayToObject;echo "

输出对象属性

";var_dump;echo "
-------------------------------
";echo "

对象转数组

";$arr=object_array;echo "

输出数组数值

";var_dump;echo "
-------------------------------
";echo "

Json到数组

";$json ='{"a":"哈哈","b":"我","c":"数","d":"一","e":3,"f":2,"g":1,"h":"别生气了"}';$arr=simple_json_parser;echo "
官方换种方法
";echo "对象
";var_dump;$a=json_decode;echo "
属性:";echo $a->a;echo "
数组
";var_dump(json_decode;echo "
-------------------------------
";echo "

数组到Json

";var_dump;echo "
";var_dump(json_encode);

好在php有json-schema模块,可以用来验证json是否符合规定的格式。

string "{"demo1":"my demo1","demo2":{"demo2_demo1":"aaaaaaaa","demo2_demo2":"bbbbbbbb","demo2_demo3":{"demo2_demo3_demo1":"ccccccc"}},"demo3":22}"array { ["demo1"]=> string "my demo1" ["demo2"]=> array { ["demo2_demo1"]=> string "aaaaaaaa" ["demo2_demo2"]=> string "bbbbbbbb" ["demo2_demo3"]=> array { ["demo2_demo3_demo1"]=> string "ccccccc" } } ["demo3"]=> int}

输出

安装使用composer

美狮美高梅官方网站,java json数据格式转换依赖包

-------------------------------现在的数组array { ["name"]=> string "李小龙" ["tel"]=> string "11111111111" ["age"]=> string "12" }-------------------------------数组转对象object { ["name"]=> string "李小龙" ["tel"]=> string "11111111111" ["age"]=> string "12" }输出对象属性string "李小龙"-------------------------------对象转数组array { ["name"]=> string "李小龙" ["tel"]=> string "11111111111" ["age"]=> string "12" }输出数组数值string "李小龙"-------------------------------Json到数组array { ["a"]=> string "哈哈" ["b"]=> string "我" ["c"]=> string "数" ["d"]=> string "一" ["e"]=> string "3" ["f"]=> string "2" ["g"]=> string "1" ["h"]=> string "别生气了" }官方换种方法对象object { ["a"]=> string "哈哈" ["b"]=> string "我" ["c"]=> string "数" ["d"]=> string ["f"]=> int ["h"]=> string "别生气了" }属性:哈哈数组array { ["a"]=> string "哈哈" ["b"]=> string "我" ["c"]=> string "数" ["d"]=> string ["f"]=> int ["h"]=> string "别生气了" }-------------------------------数组到Jsonarray { ["a"]=> string "哈哈" ["b"]=> string "我" ["c"]=> string "数" ["d"]=> string "一" ["e"]=> string "3" ["f"]=> string "2" ["g"]=> string "1" ["h"]=> string "别生气了" }string ""{"a":"u54c8u54c8","b":"u6211","c":"u6570","d":"u4e00","e":3,"f":2,"g":1,"h":"u522bu751fu6c14u4e86"}""

composer require justinrainbow/json-schema:~1.3

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,

PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

新建一个schema文件,如:schema.json

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 { index4(); } /** * 简单的simple * */ public static void index1() { JSONObject json = new JSONObject(); json.element; json.element; System.out.println; } /** * Map 数据转json * */ public static void index2() { JSONObject json = new JSONObject(); Map map = new HashMap(); map.put; map.put; json.accumulateAll; System.out.println; } /** * List 转json * */ public static void index3() { JSONArray arr = new JSONArray(); List> list = new ArrayList>(); list.add; list.add; arr.addAll; System.out.println; } /** * 对象转Json * */ public static void index4() { Demo1 demo1 = new Demo1(); demo1.setName; demo1.setAge; JSONObject json = new JSONObject(); JSONArray arr = new JSONArray(); json.element; arr.add; //json.containsKey; System.out.println; System.out.println; } private static Map getMap(String key,String val) { Map map = new HashMap; return map; }}

package Main;public class Demo1 { private String name; private int age; public void setName { this.name = name; } public String getName() { return this.name; } public void setAge { this.age = age; } public int getAge() { return this.age; }}

在线JSON代码检验、检验、美化、格式化工具:

{ "type": "object", "properties": { "firstName": { "type": "string", "required": true }, "lastName": { "type": "string" }, "age": { "type": "integer", "minimum": 0 }, "data":{ "type":"object", "required":true, "properties":{ } } }}

本文由美狮美高梅官方网站发布,转载请注明来源

关键词: