问题起因

以往都是直接用构造数组的形式构造json

例子

$arr = array("A"=>"1","B"=>"2");
$arr = json_encode($arr);
print_r($arr);

得到输出

{"A":"1","B":"2","C":"3"}

然而此时要构造这样的json:

{
"table": [
{
"id": "0",
"flow": [
{
"id": "0",
"match": {
"in-port": "1",
"vlan-match": {
"vlan-id": {
"vlan-id-present": "true",
"vlan-id": "20"
}
}
},
"instructions": {
"instruction": [
{
"apply-actions": {
"action": [
{
"output-action": {
"output-node-connector": "3",
"max-length": "65535"
},
"order": "1"
},
{
"pop-vlan-action": {},
"order": "0"
}
]
},
"order": "0"
}
]
},
"buffer_id": "65535",
"installHw": "true",
"barrier": "true",
"strict": "true",
"priority": "160",
"idle-timeout": "0",
"hard-timeout": "0",
"table_id": "0"
}
]
}
]
}

对于[] ,以及空的{}的如果构造是一个难题。

问题解决

访问到了一篇文章 php构造json的方法,并补充了构造空{}的方案

构造空{}方案如下

$arr = array("A"=>"1","B"=>"2","C"=>"3","D"=> new stdClass());
$arr = json_encode($arr);
print_r($arr);

结果

{"A":"1","B":"2","C":"3","D":{}}

综合上述方案,构造上述方案,并得到解决

$post_data = array();
$post_data["table"][0]['id'] = "0";
$post_data["table"][0]['flow'][0]["id"] = "0";
$post_data['table'][0]['flow'][0]["match"]["in-port"] = "1";
$post_data['table'][0]['flow'][0]['match']['vlan-match']['vlan-id']['vlan-id-present'] = "true";
$post_data['table'][0]['flow'][0]['match']['vlan-match']['vlan-id']['vlan-id'] = "20";
$post_data['table'][0]['flow'][0]['instructions']['instruction'][0]['apply-actions']['action'][0]['output-action']['output-node-connector'] = "3";
$post_data['table'][0]['flow'][0]['instructions']['instruction'][0]['apply-actions']['action'][0]['output-action']['max-length'] = "65535";
$post_data['table'][0]['flow'][0]['instructions']['instruction'][0]['apply-actions']['action'][0]['order'] = "1";
$post_data['table'][0]['flow'][0]['instructions']['instruction'][0]['apply-actions']['action'][1]['pop-vlan-action'] = new stdClass();
$post_data['table'][0]['flow'][0]['instructions']['instruction'][0]['apply-actions']['action'][1]['order'] = "0";
$post_data['table'][0]['flow'][0]['instructions']['instruction'][0]['order'] = "0";
$post_data['table'][0]['flow'][0]['buffer_id'] = "65535";
$post_data['table'][0]['flow'][0]['installHw'] = "true";
$post_data['table'][0]['flow'][0]['barrier'] = "true";
$post_data['table'][0]['flow'][0]['strict'] = "true";
$post_data['table'][0]['flow'][0]['priority'] = "160";
$post_data['table'][0]['flow'][0]['idle-timeout'] = "0";
$post_data['table'][0]['flow'][0]['hard-timeout'] = "0";
$post_data['table'][0]['flow'][0]['table_id'] = "0"; $post_data = json_encode($post_data);
echo $post_data;

PHP利用数组构造JSON的更多相关文章

  1. 如何利用JavaScript遍历JSON数组

    1.设计源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. jsoncpp构造json字符串和json数组

    jsoncpp构造json字符串和json数组 参考文章:Jsoncpp的简单使用 下载json文件夹放在c++项目的include目录下,在CMakeLists中include进去,然后就可以在代码 ...

  3. Java基础/利用fastjson反序列化json为对象和对象数组

    利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...

  4. java 构造json对象数组

    利用for构造 import com.google.gson.JsonArray; import com.google.gson.JsonObject; public class Test { pub ...

  5. 构造Json对象串工具类

    import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.Property ...

  6. Java下利用Jackson进行JSON解析和序列化

    Java下利用Jackson进行JSON解析和序列化   Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...

  7. json处理三部曲之第三曲:利用Gson处理json

    需要导入gson-xxx.jar包 <dependency> <groupId>com.google.code.gson</groupId> <artifac ...

  8. 两种库解析、构造 JSON

    1.用CJSON库 1.1解析Json 需要解析的JSON文件: { "name":"Tsybius", , "sex_is_male":t ...

  9. 利用Ajax和JSON实现关于查找省市名称的二级联动功能

    功能实现的思路:我们经常碰见网上购物时候填写收件地址会用到这个查找省市县的三级联动查找功能,我们可以利用Ajax和JSON技术模拟这个功能,说白了同样是使用Ajax的局部数据更新功能这个特性.因为省市 ...

随机推荐

  1. [原]点击按钮,表格隔行变色:偶数行为黄色背景,奇数行为默认颜色。通过table的getElementsByTagName取得所有的tr,依次遍历,如果是偶数就…………。

    window.onload = function () { document.getElementById('btn').onclick = function () {                 ...

  2. Elasticsearch head安装

    es5.0目前没有head插件所以不要下载错而是版本导致无法安装head; 安装head命令在es的bin目录下执行 ./plugin install mobz/elasticsearch-head ...

  3. ZOJ 2872 Binary Partitions

    先写一个完全背包,然后找规律,然后打表. #include<cstdio> #include<cstring> #include<cmath> #include&l ...

  4. Windows恢复Grub引导,用grub安装ubuntu

    http://www.linuxidc.com/wap.aspx?nid=18027&p=&cp=&cid=http://m.blog.chinaunix.net/uid-22 ...

  5. ORACLE中CHAR、VARCHAR、NVARCHAR

    1. char      固定长度,最长n个字符.   2. varchar      最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同)   char和varchar区别:   ...

  6. jquery has deprecated synchronous XMLHTTPRequest

    Like many others, my website is using jquery. When I open the developer tools, I see a warning that ...

  7. Diamond Collector

    Diamond Collector 题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining ...

  8. CALayer --> UIView

    一.CALayer和UIView的关系 UIView显示在屏幕上归功于CALayer 可以说:UIView依赖CALayer,又高于CALayer 通过调用drawRect方法来渲染自身的内容,调节C ...

  9. Java对数函数及Java对数运算

    Java对数函数及Java对数运算 2010-05-17 10:32 中国IT实验室 佚名   关键字:Java   Java对数函数的计算方法非常有问题,然而在API中却有惊人的误差.但是假如运用了 ...

  10. jstring, String, char* 变换函数

    #include <malloc.h> #include <string.h> #include <stdlib.h> #include <vcclr.h&g ...