如何将json数据转换为下面提到的格式
下面给出的数据是从xml数据转换而来的josn如何将json数据转换为下面提到的格式,json,laravel-5,laravel-4,laravel-5.2,Json,Laravel 5,Laravel 4,Laravel 5.2,下面给出的数据是从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":"SJ
{"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;
我想这就是您要找的我刚刚测试了代码,它可以正常工作
$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;
我想这就是你想要的它会奏效的
它会起作用的
它工作正常,但我需要将此数据传递到我的视图。当我使用下面的代码将数据返回到视图页面时,我遇到了错误,因为“htmlspecialchars()期望参数1为字符串,数组给定”将数据返回到视图页面的代码如下所示:
return view('clientlayout.main.test2',array('final_data'=>final_data))
您可以在视图(刀片模板)中发布访问代码吗?在刀片(视图)中查找代码:@foreach($final_data['tldlist']['tld']as$value){{{$value['Command']}@endforeach
命令位于结果数组的第一级,您在两级@foreach($final_data as$value){$value['Command']}中访问它@endforeachNow我得到一个未定义索引的错误:命令。。。我需要将整个数组传递到查看页面。当我使用这个代码{{$finaldata}}时,我得到了一个错误…它工作得很好,但我需要将这个数据传递给我的视图。当我使用下面的代码返回数据到视图页面时,我得到了一个错误,因为“htmlspecialchars()期望参数1是字符串,数组给定”返回数据到视图页面的代码如下:return view('clientlayout.main.test2',array('final_data'=>$final_data));
您可以在视图(刀片模板)中发布如何访问它的代码在刀片(视图)中查找代码:@foreach('final_data['tldlist']['tld']作为$value){{$value['Command']}@endforeach
命令位于结果数组的第一级,您在两级@foreach($final_data作为$value){{{$value['Command']}}@endforeachNow我在未定义索引时出错:Command…我需要将整个数组传递给查看页面。当我使用此代码时{$finaldata}我得到错误…返回到视图页面的代码是否正确。因为我得到的错误是“htmlspecialchars()期望参数1为字符串,数组给定”`return view('clientlayout.main.test1',array('configdata'=>$configdata));`return view('clientlayout.main.test1')->with('configdata',$configdata)
**试试这个**返回查看页面的代码是否正确。因为我得到的错误是“htmlspecialchars()期望参数1是字符串,数组给定”`return view('clientlayout.main.test1',array('configdata'=>$configdata));`返回视图('clientlayout.main.test1')->with('configdata',$configdata);
**试试这个**
$configdata = collect(json_decode(json_encode($data)))
->toArray();