/*********************************************************************
* 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. swift学习笔记 - 判断当前运行的系统和平台

    最近代码需要判断代码运行的系统与平台,下面总结了一下swift下一些可以用来判断的属性: // 代码运行在32位的 Windows public var TARGET_OS_MAC: Int32 { ...

  2. LeetCode——Palindromic Substrings

    Question Given a string, your task is to count how many palindromic substrings in this string. The s ...

  3. java和groovy的混用

    java在语言的动态性方便不是很灵活,如果你想快速增加或改变一些方法,那么只能通过反射机制,并且参数传递的格式很严格. 相比之下,基于groovy可以快速写出一些自定义方法,并能和java很好结合,类 ...

  4. 【异常记录(五)】C# 无法发送具有此谓词类型的内容正文错误

    今天请求接口直接调了以前写好的方法,结果报了(405)不支持方法的错误,一看是GET写成POST了,改成GET之后,又报了无法发送具有此谓词类型的内容正文错误的错误 原来之前的方法里面有GetRequ ...

  5. javascript 跨域访问

    JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.因为同源策略的限制,a.com 域名下的js无法操作b.com或是c.a.com域名下的对象. 下表给出了相对 http://si ...

  6. 配置servlet支持文件上传

    Servlet3.0为Servlet添加了multipart配置选项,并为HttpServletRequest添加了getPart和getParts方法获取上传文件.为了使Servlet支付文件上传需 ...

  7. js 捕获型事件

    true 为捕获型事件 false 为冒泡型事件

  8. RocketMQ学习分享

    消息队列的流派 什么是 MQ Message Queue(MQ),消息队列中间件.很多人都说:MQ 通过将消息的发送和接收分离来实现应用程序的异步和解偶,这个给人的直觉是——MQ 是异步的,用来解耦的 ...

  9. Effective C++学习笔记(1)

    最近刚看完Effective C++,记录一下当前几个比较常用的方法. 1.以独立语句将newed对象置入智能指针 智能指针是以对象管理资源,在构造函数中获得资源并在析构函数中释放资源​ 以下调用:​ ...

  10. 三大平衡树(Treap + Splay + SBT)总结+模板

    Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...