PHP convet class to json data
/*********************************************************************
* PHP convet class to json data
* 说明:
* 突然想使用class自动转换为json数据,这样的代码可扩展性会好一点,
* 只需要修改class的属性就能够达到最终json数据输出,不过有遇到class中
* 初始化class变量需要在构造函数中初始化的的问题。
*
* 2017-8-11 深圳 龙华樟坑村 曾剑锋
********************************************************************/ 一、参考文档:
. getting Parse error: syntax error, unexpected T_NEW [closed]
https://stackoverflow.com/questions/15806981/getting-parse-error-syntax-error-unexpected-t-new 二、测试代码:
<?php
class Uart {
public $port = "/dev/ttyO0";
public $value = "OK";
} class Context {
public $uart = new Uart();;
public $version = "v0.0.1";
} $context = new Context; $context_json = json_encode($context);
echo $context_json
?> 三、报错内容:
Parse error: syntax error, unexpected 'new' (T_NEW) in /usr/share/web/time.php on line 四、最终代码:
<?php
class Uart {
public $port = "/dev/ttyO0";
public $value = "OK";
} class Context {
public $uart;
public $version = "v0.0.1"; public function __construct() {
$this->uart = new Uart();
}
} $context = new Context; $context_json = json_encode($context);
echo $context_json
?> 五、输出结果:
{"uart":{"port":"\/dev\/ttyO0","value":"OK"},"version":"v0.0.1"} 六、原因:
you must do initialize new objects in the __construct function;
PHP convet class to json data的更多相关文章
- directly receive json data from javascript in mvc
if you send json data to mvc,how can you receive them and parse them more simply? you can do it like ...
- Guzzle Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
项目更新到正式平台时,出现Guzzle(5.3) client get请求出现:Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, ...
- SQL to JSON Data Modeling with Hackolade
Review: SQL to JSON data modeling First, let’s review, the main way to represent relations in a rela ...
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 co ...
- json data 解析demo
json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...
- jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后 ...
- jstree中json data 的生成
jstree中json data 的生成 jstree官网上给出的json数据格式是这样的: <span style="font-size:14px;">// A ...
- json data转匿名对象C#
using Newtonsoft.Json.Linq; 代码如下: static void Main(string[] args) { Console.WriteLine("Test 4.8 ...
随机推荐
- Commons Configuration之一简介
转载自(https://my.oschina.net/u/2000201/blog/486327) 1 简介 Commons Configuration软件类库提供通用配置接口,使Java应用程 ...
- Servlet3.0与Spring
servlet filter listener web.xml DispatcherServlet web.xml 后期 servlet3.0我们去除web.xml 通过注解方式 同时需要s ...
- C# DES加密,KEY和IV不同设置的写法
1.KEY和IV分别赋值 //默认密钥向量 private static byte[] Iv= { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; ...
- Yii框架(一)
这里接触了 MVC 设计模式中的控制器和视图部分. 创建了一个操作作为控制器的一部分去处理特定请求. 然后又创建了一个视图去构造响应内容. 在这个小例子中,没有模型调用,唯一涉及到数据的地方是 mes ...
- ”由于没有远程桌面授权服务器可以提供许可证,远程会话被中断“的解决方案
由于windows server 2012 R2 Datacenter 安装了 远程桌面角色,但是这个角色是120天免费的,需要购买授权的. 解决方案: 删除这个角色,就可以正常进行远程桌面连接了.但 ...
- Redis可以做哪些事儿?
Redis可以作为数据库,提供高速缓存,消息队列等功能,这里介绍Redis可以做的其中两件事: 1.提供缓存功能,作为缓存服务器; 2.轻量级的消息队列(MQ)进行使用. /// <summar ...
- photoshop CS5制作具有立体感的按钮
今天在学习用photoshop cs5制作html模板的过程中,遇到了立体感按钮的制作问题.当然按钮的立体感也可以用CSS来实现,这里主要是用PS来制作具有立体感的按钮. 我也是PS新手,下面的东西, ...
- Jenkins基础复习
- tarball安装GnuPG (gpg) 2.2.10
https://www.gnupg.org/download/ mac 方式一:推荐 mac $ brew install gpg pinentry pinentry-mac $ echo " ...
- 第六天 vim编辑的使用和Xmanager远程工具的使用
1.1 vim主要模式介绍,vim命令模式 使用命令 vim [file name] 有三种主要模式:命令模式.编辑模式.命令行模式 在vim中主要使用快捷键进行操作,详见:http://www.cn ...