如何将json数据转换为以下提到的格式 [英] How to convert the json data to the below mentioned format

查看:149
本文介绍了如何将json数据转换为以下提到的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出的数据是从xml数据转换的josn.

The data given below is the josn converted from xml data.

{"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":{}}

我需要将以上数据转换为以下提到的格式:

I need to convert the above data to the format mentioned below:

  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为字符串,给定数组".

Suggest me solution to get the data in the mentioned format.I need the json data in the decoded format in my view.when I use json_decode in the controller I'm getting error as "htmlspecialchars() expects parameter 1 to be string, array given".

推荐答案

我刚刚测试了代码,并且可以正常工作

I have just tested the code and it works

$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
        (
        )

)

我认为这就是您要寻找的

I think this is what you are looking for

这篇关于如何将json数据转换为以下提到的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆