/*********************************************************************
* 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. JavaConfig 使用Java代码进行显示配置

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. 需要先加载spring-context 包 &l ...

  2. 建站有很多技术,如 HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、http://ASP.NET、Web Services、浏览器脚本、服务器脚本等。它们的区别是什么?新手一点不懂,想理清所有这些技术之间的关系和应用范围。

    先普及用户通过 浏览器 访问网页 的过程: 网页内容是通过服务器运算得出的结果,将结果(网页代码)传输给浏览器,网页代码再通过浏览器运算(计算.渲染),最终展示在用户的眼前的. 至此,我们知道了有2个 ...

  3. Sublime使用记录之SublimeServer插件介绍

    Sublime使用记录之SublimeServer插件介绍 介绍:sublimeServer

  4. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Sum

    2017-09-16 12:13:44 writer:pprp 特判 #include <iostream> using namespace std; int main() { int c ...

  5. Python学习札记(六) Basic3 List和Tuple

    参考:List Tuple Note List List是Python中一个很吊的数据结构,类似C语言的数组. 1.定义:listname = [variable 1, v2, v3, ..., vn ...

  6. rownum和order by的执行顺序问题

    SQL中rownum和order by的执行顺序的问题 : 在一个SQL中,如果同时使用rownum和order by,会有一个先后顺序的问题. 比如select id1,id2 from t_tab ...

  7. 使用iview--1

    在任意一个你想创建项目的路径下 每次输入就输一致的就可以 /*************************安装选项开始****************/ 回车再次回车就如下,输入Y 继续 回车,输 ...

  8. Jenkins的安装和使用

    1.可以参考W3C----https://www.w3cschool.cn/jenkins/jenkins-5h3228n2.html 两种方式安装Jenkins a.安装包 b.Jenkins.wa ...

  9. Android------底部导航栏BottomNavigationBar

    Android 的底部导航栏 BottomNavigationBar 由Google官方Material design中增加的. Android底部导航栏的实现方式特别多,例如TabHost,TabL ...

  10. 1-10 RHLE7 系统进程管理

    1.1-Linux进程管理 程序.进程.线程 程序:一组指令的集合    QQ 进程:程序的执行就是进程.也可以把进程看成一个独立的程序,在内存中有其对应的代码空间和数据空间,一个进程所拥有的数据和代 ...