79234

How to convert the json data to the below mentioned format

Question:

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

Find my controller code:

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 )); }

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".

Answer1:

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;

And below is the output i got,

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

Answer2:

It Will work.

$configdata = collect(json_decode(json_encode($data))) ->toArray();

Recommend

  • Crystal Reports formula field: IF ISNULL(decimal) THEN 0.00 … does not work correctly
  • CONCAT ON Query in join more two table mysql table
  • Unable to delete HDFS Corrupt files
  • Prepopulate tabularinline with value from related lookup in manytomany field
  • Django Aggreagtion: Sum return value only?
  • Highlight cell when row is selected
  • jQuery window.scroll move div vertical in opposite direction
  • can I capture remotely executed commands with its output in the logfile in python?
  • Load PointerLockControl camera view to another camera
  • How to use predicate transpose in SWI-Prolog?
  • Removing Small Regions from a Binary Image
  • Set a Default value for each empty XML tags in XSLT 1.0
  • Why is 31 >= 20 returning False here when comparing day?
  • Currency Format with Knockout
  • Mysql grant all privileges is not getting applied
  • Postgresql huge performance difference when using IN vs NOT IN
  • Python how to format currency string
  • AutoMapper doesn't map calculated field to scalar
  • Display product cost in front end
  • Dictionary of Linq Expression [duplicate]
  • how to return an object (from a MySQL temporary table) into a Coldfusion stored procedure?
  • PdfBox issue while changing page
  • Ruby spreadshet gem, how can I center align a number
  • Show indexes in MySQL table
  • SQL Server equivalent of Excel's TINV function
  • Error casting JSON to custom object
  • How to add totals to each group in SSRS
  • Why is indexPath for section 1 of a tableview starting with a value of [1,0]
  • mySQL INSERT IGNORE doesn't “ignore”
  • SSRS expression throws #Error - Possible reasons?
  • How is transparency actually implemented ?
  • R distribute weights over a vector
  • How can I handle Castor unmarshaling of SOAP messages when the namespace is defined inside the opera
  • roundoff double values in Java [duplicate]
  • GeoCoordinateWatcher is not getting GeoCoordinate Speed in emulator?
  • Excel VBA - add a custom number format
  • How to get desired format from ArrayList
  • two ways of displaying a decimal
  • Globalized custom number formatting - Variable decimal points
  • Why is it still possible to insert a foreign key that doesn't exist?