【问题标题】:How to create a JSON object with nested object and array in php [closed]如何在php中创建具有嵌套对象和数组的JSON对象[关闭]
【发布时间】:2017-10-19 07:41:47
【问题描述】:

这就是我希望我的对象看起来的样子:

{
    "cluster": "testcluster",
    "instance": "i-03f8f8c9e7febab21",
    "instype": "r4.8xlarge",
    "AZ": "us-east-1e",
    "env": "test", 
    "cpus":  "32",
    "memory": "256G",
    "kernel": "4.4.0-96-generic",
    "info": "cpu usage stats",
    "unit": "percent",
    "cpustats": [
          {"metric":"user", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"sys",  "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"idle", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"softirq", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"intr", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"steal", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
          {"metric":"user", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]}
   ]
}

如何在php中构建?

【问题讨论】:

  • 首先显示您的代码。我们不是代码生成器。
  • 这是你最好的新朋友json formatter + validation
  • 第 1 步:进行大量研究。第2步:尝试一些东西。第 3 步:如果您遇到特定问题而无法在线找到解决方案,请返回并向我们展示您尝试过的内容、遇到的问题、预期输出和实际输出,我们可以从那里帮助您。

标签: php arrays json nested


【解决方案1】:

对不起,这个问题的解释真的很糟糕!如果我理解,你想要一个像上面这样的 JSON(你的帖子)

PHP 有一个特殊的函数来管理 JSON 对象:json_encode AND json_decode


使用 json_encode

用于将Object转成JSON字符串

<?php
/** You can also use Arrays (Ex. $myArray = array(...)) */
$myObject = new StdClass();

$myObject->newKey = "newValue";
$myObject->otherKey = "otherValue";

$myObject->nestedArrays = array(
    'array1' => array(
        'key1' => 'value1',
        'key2' => 'value2', 
        /** ... OTHER KEY-VALUE ... */
        'keyN' => 'vakyeN'
    ),

    'array2' => array(
        'key1' => 'value1',
        'key2' => 'value2', 
        /** ... OTHER KEY-VALUE ... */
        'keyN' => 'vakyeN'
    )
);

/** ... OTHER CODE ... */

$myJSONString = json_encode($myObject);

print_r($myJSONString);

/** OUTPUT
  * {
  *     "newKey":"newValue",
  *     "otherKey":"otherValue",
  *     "nestedArrays":{
  *         "array1":{
  *             "key1":"value1",
  *             "key2":"value2",
  *             "keyN":"vakyeN"
  *         },
  *         "array2":{
  *             "key1":"value1",
  *             "key2":"value2",
  *             "keyN":"vakyeN"
  *         }
  *     }
  * }
  */

?>

使用 json_decode

用于从 JSON 字符串中检索原始对象

<?php

$myObject = json_decode($myJSONString);

?>

现在尝试为您的帖子创建 JSON 字符串 =)

我尝试使用你的想法:

我更改了 JSON 的外观:

{
    "cluster": "testcluster",
    "instance": "i-03f8f8c9e7febab21",
    "instype": "r4.8xlarge",
    "AZ": "us-east-1e",
    "env": "test", 
    "cpus":  "32",
    "memory": "256G",
    "kernel": "4.4.0-96-generic",
    "info": "cpu usage stats",
    "unit": "percent",
    "cpustats": {
          "user":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "sys":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "idle":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "softirq":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "intr":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
          "steal":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]
    }
}

代码:

$obj = new stdClass();
$obj->cluster="testcluster";
$obj->instance="i-03f8f8c9e7febab21";
$obj->instype="r4.8xlarge";
$obj->AZ="us-east-1e",
$obj->env="test",
$obj->cpus="32";
$obj->memory="25G";
$obj->kernelversion="4.4.0-96-generic";
$obj->info="cpu usage stats";
$obj->unit="percent";
$obj->cpustats = new stdClass();
$obj->cpustats->usr = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->sys = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->idle = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->softirq = array(1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->intr = array (1874,1857,1884,1869,1909,1912,1901,1880,1883); 
$obj->cpustats->steal = array (1874,1857,1884,1869,1909,1912,1901,1880,1883); 

var_dump(json_encode($obj));

输出:

string(555) "{
"cluster":"testcluster",
"instance":"i-03f8f8c9e7febab21",
"instype":"r4.8xlarge",
"AZ":"us-east1e",
"env":"test",
"cpus":"32",
"memory":"25G",
"kernelversion":"4.4.0-96-generic",
"info":"cpu usage stats",
"unit":"percent",
"cpustats":{
"usr":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"sys":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"idle":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"softirq":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"intr":[1874,1857,1884,1869,1909,1912,1901,1880,1883],
"steal":[1874,1857,1884,1869,1909,1912,1901,1880,1883]
}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多