/*********************************************************************
* 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的更多相关文章

  1. 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 ...

  2. 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, ...

  3. 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 ...

  4. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决

    记录个报错: 问题描述: 经过服务器生成图片返到前台时,在火狐浏览器中下载图片或打开图片时报错:SyntaxError: JSON.parse: unexpected character at lin ...

  5. 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 ...

  6. json data 解析demo

    json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...

  7. 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解析后 ...

  8. jstree中json data 的生成

       jstree中json data 的生成 jstree官网上给出的json数据格式是这样的: <span style="font-size:14px;">// A ...

  9. json data转匿名对象C#

    using Newtonsoft.Json.Linq; 代码如下: static void Main(string[] args) { Console.WriteLine("Test 4.8 ...

随机推荐

  1. CSS控制滚动条的样式

    到今天(2018年10月25日)为止, 这还是chrome上的一个实验性特性: ::-webkit-scrollbar{width:4px;height:4px;} ::-webkit-scrollb ...

  2. Forcing restore from package sources

    https://docs.microsoft.com/en-us/nuget/consume-packages/package-restore#forcing-restore-from-package ...

  3. 第八篇:Spark SQL Catalyst源码分析之UDF

    /** Spark SQL源码分析系列文章*/ 在SQL的世界里,除了官方提供的常用的处理函数之外,一般都会提供可扩展的对外自定义函数接口,这已经成为一种事实的标准. 在前面Spark SQL源码分析 ...

  4. 数据结构实习 - Problem N 树的括号表示法

    writer:pprp date:20171103 题目描述 先将根结点放入一对圆括号中,然后把它的子树按由左而右的顺序放入括号中,而对子树也采用同样方法处理:同层子树与它的根结点用圆括号括起来,同层 ...

  5. Spring Cloud 坑点

    1 配置中心 1.config 默认Git加载 通过spring.cloud.config.server.git.uri指定配置信息存储的git地址,比如:https://github.com/spr ...

  6. 《用 Python 学微积分》笔记 2

    <用 Python 学微积分>原文见参考资料 1. 13.大 O 记法 比较两个函数时,我们会想知道,随着输入值 x 的增长或减小,两个函数的输出值增长或减小的速度究竟谁快谁慢.通过绘制函 ...

  7. mongodb安装、运行

    1.下载安装: 切换到:/usr/local/ mkdir -p mongodb groupadd -g 800 mongodb useradd -u 801 -g mongodb mongodb c ...

  8. vs 2017 保存文件 utf8

    vs 2017 保存文件 utf8 转自:https://blog.csdn.net/jiegemena/article/details/79369650

  9. wget下载指定目录下的文件

    wget -r -np -k -P  ~/tmp/    http://xxx.com/download -P 表示下载到哪个目录-r 表示递归下载-np 表示不下载旁站连接.-k 表示将下载的网页里 ...

  10. 搞懂分布式技术19:使用RocketMQ事务消息解决分布式事务

    搞懂分布式技术19:使用RocketMQ事务消息解决分布式事务 初步认识RocketMQ的核心模块 rocketmq模块 rocketmq-broker:接受生产者发来的消息并存储(通过调用rocke ...