【问题标题】:How to use php and jquery to produce a multilevel json structure如何使用php和jquery生成多级json结构
【发布时间】:2017-03-12 07:29:44
【问题描述】:

我想产生如下输出,但我不知道该怎么做,有什么帮助吗?

{  
   "messages":[  
      {  
         "from":"InfoSMS",
         "to":[  
            "41793026727",
            "41793026731"
         ],
         "text":"May the Force be with you!"
      },
      {  
         "from":"41793026700",
         "to":"41793026785",
         "text":"A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
      }
   ]
}

【问题讨论】:

  • 你尝试了什么?
  • 您的问题到底是什么?你想用 PHP 还是 jQuery 创建这个结构?您如何存储原始数据?
  • 是的,我想要可以产生上述输出的 php 和 jquery 代码。
  • Stackoverflow 不是“如何” 教程服务

标签: javascript php jquery json


【解决方案1】:

我假设您指的是它们的语法有何不同。我首先提供了一种在 PHP 中定义它的方法,然后我向您展示了一种在 JavaScript 中定义它的方法:

<?php

$json = Array( 
   "messages" => Array(   
      Array( 
         "from" => "InfoSMS",
         "to" => Array(   
            "41793026727",
            "41793026731"
         ),
         "text" => "May the Force be with you!"
      ),
      Array(   
         "from" => "41793026700",
         "to" => "41793026785",
         "text" => "A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
      )
   )
);

print_r($json);

?>

<script>

json = {  
   "messages":[  
      {  
         "from":"InfoSMS",
         "to":[  
            "41793026727",
            "41793026731"
         ],
         "text":"May the Force be with you!"
      },
      {  
         "from":"41793026700",
         "to":"41793026785",
         "text":"A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
      }
   ]
};

console.log(json);

</script>

PHP 的 eval.in:

https://eval.in/753090

Javascript:

https://eval.in/753091

【讨论】:

    【解决方案2】:

    您可以在 PHP 数组上使用 json_encode() 将其转换为 JSON。您需要从数据集中生成类似的内容。

    $data = [
        'messages'=> [
            [
                'from' => 'InfoSMS',
                'to' => [
                    '41793026727',
                    '41793026731',
                ],
                'text' => 'May the Force be with you!',
            ],
            [
                'from' => '41793026700',
                'to' => [
                    '41793026785',
                ],
                'text' => 'A long time ago, in a galaxy far, far away... It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire.',
            ],
        ],
    ];
    
    echo json_encode($data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 2017-05-12
      • 2013-03-29
      • 1970-01-01
      • 2014-06-12
      相关资源
      最近更新 更多