/*********************************************************************
* 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. POJ-1458 LCS(线性动态规划)

    此题经典线性动态规划. 代码如下: #include<iostream> #include<cstdio> #include<cstdlib> #include&l ...

  2. Oracle邮件推送函数

    CREATE OR REPLACE PROCEDURE PROCSENDEMAIL ( P_TXT VARCHAR2, P_SUB VARCHAR2, P_SENDOR VARCHAR2, P_REC ...

  3. 【eclipse】Multiple annotations found at this line:——解决方法

    问题截图: 就是eclipse的maven插件太旧了 用新插件新建的maven项目就没有报错 用软件对比了一下这两个pom文件 只有项目名有区别 所以就是插件的问题 一个简单安装离线maven插件的方 ...

  4. 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出

    用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...

  5. [C# 开发技巧系列]如何动态设置屏幕分辨率

    首先,大家应该明确,现在没有可用的API来给我们动态地设置屏幕分辨率,我们要实现这个需求,我们只能在C#程序中调用Win32 API 函数来解决这个问题的,这里用C#代码调用Win32 API 就涉及 ...

  6. js原码工具集

    /* 原生js工具集 */ arr是否包含obj function contains1(arr, obj) { var i = arr.length; while (i--) { if (arr[i] ...

  7. Python+Opencv进行识别相似图片

    http://blog.csdn.net/feimengjuan/article/details/51279629

  8. poj1228稳定凸包

    就是给一系列点,看这是不是一个稳定凸包 稳定凸包是指一个凸包不能通过加点来使它扩大面积,也就是说每条边最少有三个点 判断的地方写错了,写了两边循环,其实数组s已经排好了序,直接每三个判断就好了 #in ...

  9. 1-17-Linux中计划任务与日志的管理

    本节所讲内容: 1-1 Linux中的计划任务 1-1-1 at计划任务的使用 1-1-2 cron 计划任务的使用 1-1 Linux服务器的日志管理 1-1-1 日志的种类和记录的方式 1-1-2 ...

  10. HDU5521-最短路-建图

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...