Convert php object to nested Json [closed]











up vote
-5
down vote

favorite












I am new in PHP. I have text file with list of PHP objects arrays in it. I want to write a PHP script to read the rows in the file, convert it using json_encode function and write the converted JSON data into a new file.



Here is the PHP code I have come up with. But obviously not working and returning null.



<?php 
$file = fopen("sampleobjects.txt", "r");
$raw_text = fread($file,filesize("sampleobjects.txt"));

foreach ($raw_text as $item) {
$json = json_encode($item, true);
}

//print_r(htmlspecialchars($json));
var_dump ($json);
$converted_file = fopen("jsonconverted.txt", "w");
fwrite($converted_file, $json);
?>


Please share if you have any thoughts on how to fix it.



Thanks.



As requested, here is a sample.



 $structure=new stdClass;
$structure->children=array();
$structure->title='From 4.4.4 to the end of Body 4.4.5.1';
$structure->level='0';
$structure->course='SomeMediaName';
$structure->websites='';
$structure->lang='';
$structure->copyright='';
$structure->children=(object)array('id'=>22775381,
'title'=>'4.4.4 Title1',
'level'=>'1',
'number'=>'1',
'children'=>array(
(object)array('id'=>22775382,
'title'=>'4.4.4.1 Subtitle1',
'level'=>'2',
'number'=>'1.1',
'children'=>array()),
(object)array('id'=>22775383,
'title'=>'4.4.4.2 Subtitle2',
'level'=>'2',
'number'=>'1.2',
children'=>array()),

(object)array('id'=>22775391,
'title'=>'4.4.4.10 Subtitle3',
'level'=>'2',
'number'=>'1.10',
'children'=>array()
)));
$structure->children=(object)array(
'id'=>22775392,
'title'=>'4.4.5 Title2',
'level'=>'1',
'number'=>'2',
'children'=>array
(
(
object)array(
'id'=>22775393,
'title'=>'4.4.5.1 Subtitle1',
'level'=>'2',
'number'=>'2.1',
'children'=>array()
)
)
);
$structure->warnings=0;
$structure->authorcomments=0;
$structure->editorcomments=0;
$structure->olinksections=array();
$structure->ipud=array();









share|improve this question















closed as off-topic by Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly, Agnius Vasiliauskas Nov 19 at 17:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    what do you mean by "list of php objects arrays in it"? Provide some sample of the content of the file.
    – Dekel
    Nov 19 at 16:08






  • 1




    So if it is obviously not working - you can tell what should be fixed.
    – u_mulder
    Nov 19 at 16:08






  • 2




    You're overwriting the $json var each time. Store all the items in an array (with keys as necessary), then json_encode the array once at the end.
    – JonJ
    Nov 19 at 16:12






  • 1




    you also probably dont' mean json_encode($item,true), not per docs ... possible confusion with json_decode's syntax
    – YvesLeBorg
    Nov 19 at 16:15












  • I have added an example to the post. thanks
    – Anbarasan Dhanushkodi
    Nov 19 at 16:51















up vote
-5
down vote

favorite












I am new in PHP. I have text file with list of PHP objects arrays in it. I want to write a PHP script to read the rows in the file, convert it using json_encode function and write the converted JSON data into a new file.



Here is the PHP code I have come up with. But obviously not working and returning null.



<?php 
$file = fopen("sampleobjects.txt", "r");
$raw_text = fread($file,filesize("sampleobjects.txt"));

foreach ($raw_text as $item) {
$json = json_encode($item, true);
}

//print_r(htmlspecialchars($json));
var_dump ($json);
$converted_file = fopen("jsonconverted.txt", "w");
fwrite($converted_file, $json);
?>


Please share if you have any thoughts on how to fix it.



Thanks.



As requested, here is a sample.



 $structure=new stdClass;
$structure->children=array();
$structure->title='From 4.4.4 to the end of Body 4.4.5.1';
$structure->level='0';
$structure->course='SomeMediaName';
$structure->websites='';
$structure->lang='';
$structure->copyright='';
$structure->children=(object)array('id'=>22775381,
'title'=>'4.4.4 Title1',
'level'=>'1',
'number'=>'1',
'children'=>array(
(object)array('id'=>22775382,
'title'=>'4.4.4.1 Subtitle1',
'level'=>'2',
'number'=>'1.1',
'children'=>array()),
(object)array('id'=>22775383,
'title'=>'4.4.4.2 Subtitle2',
'level'=>'2',
'number'=>'1.2',
children'=>array()),

(object)array('id'=>22775391,
'title'=>'4.4.4.10 Subtitle3',
'level'=>'2',
'number'=>'1.10',
'children'=>array()
)));
$structure->children=(object)array(
'id'=>22775392,
'title'=>'4.4.5 Title2',
'level'=>'1',
'number'=>'2',
'children'=>array
(
(
object)array(
'id'=>22775393,
'title'=>'4.4.5.1 Subtitle1',
'level'=>'2',
'number'=>'2.1',
'children'=>array()
)
)
);
$structure->warnings=0;
$structure->authorcomments=0;
$structure->editorcomments=0;
$structure->olinksections=array();
$structure->ipud=array();









share|improve this question















closed as off-topic by Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly, Agnius Vasiliauskas Nov 19 at 17:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    what do you mean by "list of php objects arrays in it"? Provide some sample of the content of the file.
    – Dekel
    Nov 19 at 16:08






  • 1




    So if it is obviously not working - you can tell what should be fixed.
    – u_mulder
    Nov 19 at 16:08






  • 2




    You're overwriting the $json var each time. Store all the items in an array (with keys as necessary), then json_encode the array once at the end.
    – JonJ
    Nov 19 at 16:12






  • 1




    you also probably dont' mean json_encode($item,true), not per docs ... possible confusion with json_decode's syntax
    – YvesLeBorg
    Nov 19 at 16:15












  • I have added an example to the post. thanks
    – Anbarasan Dhanushkodi
    Nov 19 at 16:51













up vote
-5
down vote

favorite









up vote
-5
down vote

favorite











I am new in PHP. I have text file with list of PHP objects arrays in it. I want to write a PHP script to read the rows in the file, convert it using json_encode function and write the converted JSON data into a new file.



Here is the PHP code I have come up with. But obviously not working and returning null.



<?php 
$file = fopen("sampleobjects.txt", "r");
$raw_text = fread($file,filesize("sampleobjects.txt"));

foreach ($raw_text as $item) {
$json = json_encode($item, true);
}

//print_r(htmlspecialchars($json));
var_dump ($json);
$converted_file = fopen("jsonconverted.txt", "w");
fwrite($converted_file, $json);
?>


Please share if you have any thoughts on how to fix it.



Thanks.



As requested, here is a sample.



 $structure=new stdClass;
$structure->children=array();
$structure->title='From 4.4.4 to the end of Body 4.4.5.1';
$structure->level='0';
$structure->course='SomeMediaName';
$structure->websites='';
$structure->lang='';
$structure->copyright='';
$structure->children=(object)array('id'=>22775381,
'title'=>'4.4.4 Title1',
'level'=>'1',
'number'=>'1',
'children'=>array(
(object)array('id'=>22775382,
'title'=>'4.4.4.1 Subtitle1',
'level'=>'2',
'number'=>'1.1',
'children'=>array()),
(object)array('id'=>22775383,
'title'=>'4.4.4.2 Subtitle2',
'level'=>'2',
'number'=>'1.2',
children'=>array()),

(object)array('id'=>22775391,
'title'=>'4.4.4.10 Subtitle3',
'level'=>'2',
'number'=>'1.10',
'children'=>array()
)));
$structure->children=(object)array(
'id'=>22775392,
'title'=>'4.4.5 Title2',
'level'=>'1',
'number'=>'2',
'children'=>array
(
(
object)array(
'id'=>22775393,
'title'=>'4.4.5.1 Subtitle1',
'level'=>'2',
'number'=>'2.1',
'children'=>array()
)
)
);
$structure->warnings=0;
$structure->authorcomments=0;
$structure->editorcomments=0;
$structure->olinksections=array();
$structure->ipud=array();









share|improve this question















I am new in PHP. I have text file with list of PHP objects arrays in it. I want to write a PHP script to read the rows in the file, convert it using json_encode function and write the converted JSON data into a new file.



Here is the PHP code I have come up with. But obviously not working and returning null.



<?php 
$file = fopen("sampleobjects.txt", "r");
$raw_text = fread($file,filesize("sampleobjects.txt"));

foreach ($raw_text as $item) {
$json = json_encode($item, true);
}

//print_r(htmlspecialchars($json));
var_dump ($json);
$converted_file = fopen("jsonconverted.txt", "w");
fwrite($converted_file, $json);
?>


Please share if you have any thoughts on how to fix it.



Thanks.



As requested, here is a sample.



 $structure=new stdClass;
$structure->children=array();
$structure->title='From 4.4.4 to the end of Body 4.4.5.1';
$structure->level='0';
$structure->course='SomeMediaName';
$structure->websites='';
$structure->lang='';
$structure->copyright='';
$structure->children=(object)array('id'=>22775381,
'title'=>'4.4.4 Title1',
'level'=>'1',
'number'=>'1',
'children'=>array(
(object)array('id'=>22775382,
'title'=>'4.4.4.1 Subtitle1',
'level'=>'2',
'number'=>'1.1',
'children'=>array()),
(object)array('id'=>22775383,
'title'=>'4.4.4.2 Subtitle2',
'level'=>'2',
'number'=>'1.2',
children'=>array()),

(object)array('id'=>22775391,
'title'=>'4.4.4.10 Subtitle3',
'level'=>'2',
'number'=>'1.10',
'children'=>array()
)));
$structure->children=(object)array(
'id'=>22775392,
'title'=>'4.4.5 Title2',
'level'=>'1',
'number'=>'2',
'children'=>array
(
(
object)array(
'id'=>22775393,
'title'=>'4.4.5.1 Subtitle1',
'level'=>'2',
'number'=>'2.1',
'children'=>array()
)
)
);
$structure->warnings=0;
$structure->authorcomments=0;
$structure->editorcomments=0;
$structure->olinksections=array();
$structure->ipud=array();






php arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 16:50

























asked Nov 19 at 16:07









Anbarasan Dhanushkodi

164




164




closed as off-topic by Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly, Agnius Vasiliauskas Nov 19 at 17:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly, Agnius Vasiliauskas Nov 19 at 17:17


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Marcin Orlowski, YvesLeBorg, ADyson, RiggsFolly

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    what do you mean by "list of php objects arrays in it"? Provide some sample of the content of the file.
    – Dekel
    Nov 19 at 16:08






  • 1




    So if it is obviously not working - you can tell what should be fixed.
    – u_mulder
    Nov 19 at 16:08






  • 2




    You're overwriting the $json var each time. Store all the items in an array (with keys as necessary), then json_encode the array once at the end.
    – JonJ
    Nov 19 at 16:12






  • 1




    you also probably dont' mean json_encode($item,true), not per docs ... possible confusion with json_decode's syntax
    – YvesLeBorg
    Nov 19 at 16:15












  • I have added an example to the post. thanks
    – Anbarasan Dhanushkodi
    Nov 19 at 16:51














  • 1




    what do you mean by "list of php objects arrays in it"? Provide some sample of the content of the file.
    – Dekel
    Nov 19 at 16:08






  • 1




    So if it is obviously not working - you can tell what should be fixed.
    – u_mulder
    Nov 19 at 16:08






  • 2




    You're overwriting the $json var each time. Store all the items in an array (with keys as necessary), then json_encode the array once at the end.
    – JonJ
    Nov 19 at 16:12






  • 1




    you also probably dont' mean json_encode($item,true), not per docs ... possible confusion with json_decode's syntax
    – YvesLeBorg
    Nov 19 at 16:15












  • I have added an example to the post. thanks
    – Anbarasan Dhanushkodi
    Nov 19 at 16:51








1




1




what do you mean by "list of php objects arrays in it"? Provide some sample of the content of the file.
– Dekel
Nov 19 at 16:08




what do you mean by "list of php objects arrays in it"? Provide some sample of the content of the file.
– Dekel
Nov 19 at 16:08




1




1




So if it is obviously not working - you can tell what should be fixed.
– u_mulder
Nov 19 at 16:08




So if it is obviously not working - you can tell what should be fixed.
– u_mulder
Nov 19 at 16:08




2




2




You're overwriting the $json var each time. Store all the items in an array (with keys as necessary), then json_encode the array once at the end.
– JonJ
Nov 19 at 16:12




You're overwriting the $json var each time. Store all the items in an array (with keys as necessary), then json_encode the array once at the end.
– JonJ
Nov 19 at 16:12




1




1




you also probably dont' mean json_encode($item,true), not per docs ... possible confusion with json_decode's syntax
– YvesLeBorg
Nov 19 at 16:15






you also probably dont' mean json_encode($item,true), not per docs ... possible confusion with json_decode's syntax
– YvesLeBorg
Nov 19 at 16:15














I have added an example to the post. thanks
– Anbarasan Dhanushkodi
Nov 19 at 16:51




I have added an example to the post. thanks
– Anbarasan Dhanushkodi
Nov 19 at 16:51

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Brusque (Aveyron)

python multithreading application for fetching messages from rabbitmq

Serbische Revolution