json - 如何将 json 数据转换为下面提到的格式
问题描述
下面给出的数据是由xml数据转换而来的josn。
{"tldlist":{"tld":[{"tld":"co.uk"},{"tld":"eu"},{"tld":"live"},{"tld":{}}],"tldcount":"4"},"Command":"GETTLDLIST","APIType":"API","Language":"eng","ErrCount":"0","ResponseCount":"0","MinPeriod":{},"MaxPeriod":"10","Server":"SJL1VWRESELL_T","Site":"eNom","IsLockable":{},"IsRealTimeTLD":{},"TimeDifference":"+0.00","ExecTime":"0.000","Done":"true","TrackingKey":"b3c16684-c533-4947-b40a-19a5b4c08a31","RequestDateTime":"5\/10\/2018 12:54:28 AM","debug":{}}
我需要将上面的数据转换成下面提到的格式:
array (
'tldlist' =>
array (
'tld' =>
array (
0 =>
array (
'tld' => 'co.uk',
),
1 =>
array (
'tld' => 'eu',
),
2 =>
array (
'tld' => 'live',
),
3 =>
array (
'tld' =>
array (
),
),
),
'tldcount' => '4',
),
'Command' => 'GETTLDLIST',
'APIType' => 'API',
'Language' => 'eng',
'ErrCount' => '0',
'ResponseCount' => '0',
'MinPeriod' =>
array (
),
'MaxPeriod' => '10',
'Server' => 'SJL1VWRESELL_T',
'Site' => 'eNom',
'IsLockable' =>
array (
),
'IsRealTimeTLD' =>
array (
),
'TimeDifference' => '+0.00',
'ExecTime' => '0.000',
'Done' => 'true',
'TrackingKey' => 'b3c16684-c533-4947-b40a-19a5b4c08a31',
'RequestDateTime' => '5/10/2018 12:54:28 AM',
'debug' =>
array (
),
)
找到我的控制器代码:
public function test(){
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=gettldlist&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
return view('clientlayout.main.test1', array('configdata' =>
$configdata ));
}
建议我以上述格式获取数据的解决方案。我需要在我的视图中解码格式的 json 数据。当我在控制器中使用 json_decode 时出现错误,因为“htmlspecialchars() 期望参数 1 是字符串、数组给定”。
解决方案
我刚刚测试了代码,它可以工作
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=gettldlist&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);// Use true to get data in array rather than object
echo "<pre>";print_r($final_data);exit;
下面是我得到的输出,
Array
(
[tldlist] => Array
(
[tld] => Array
(
[0] => Array
(
[tld] => co.uk
)
[1] => Array
(
[tld] => eu
)
[2] => Array
(
[tld] => live
)
[3] => Array
(
[tld] => Array
(
)
)
)
[tldcount] => 4
)
[Command] => GETTLDLIST
[APIType] => API
[Language] => eng
[ErrCount] => 0
[ResponseCount] => 0
[MinPeriod] => Array
(
)
[MaxPeriod] => 10
[Server] => SJL1VWRESELL_T
[Site] => eNom
[IsLockable] => Array
(
)
[IsRealTimeTLD] => Array
(
)
[TimeDifference] => +0.00
[ExecTime] => 0.000
[Done] => true
[TrackingKey] => 2b71ef9f-005e-4a33-a66f-3f1f69188f1f
[RequestDateTime] => 5/10/2018 4:11:02 AM
[debug] => Array
(
)
)
我想这就是你要找的
推荐阅读
- postgresql - 用户定义的 WHERE 子句安全性
- npm - VSCode Launch.json -- 使用全局安装的 jest
- html - 如何正确测量 Angular 中的 svg 组尺寸?
- c++ - 将 __m256i 寄存器转换为 uint64_t 位掩码,以便每个字节值处的值是输出中的设置位
- javascript - 不生成随机数
- javascript - 为什么更改后对连接附加的操作会影响两个数组?
- opengl - Shadowmap 适用于正射投影,但不适用于透视投影
- node.js - 在部署到 Heroku 之前,我应该对 MERN 应用程序的 package.json 文件的脚本进行哪些更改?
- reactjs - Formik 检查复选框是否被选中
- django - Django FormModel 验证