Change default decoding structure from array to object
halaxa opened this issue · comments
This will make it more predictable as json_decode
works the same way.
The only thing needed to do is to change the line with default instantiation of ExtJsonDecoder
in Parser
.
Huge BC break - will wait to version 1.0
Sorry to hijack your thread, BY default JSONMachine FROM File return Object right now.
How do i return as Array?
$x = JsonMachine::fromFile('myjson');
echo(gettype($x) . PHP_EOL);
object
The $x
in your example is of type object
because it is a lazy Iterator. When you start iterating it in foreach
, the individual items will be arrays. It will not produce arrays until you start iterating the object.
$x = JsonMachine::fromFile('myjson');
foreach ($x as $item) {
echo(gettype($item)); // array
}
I see thank you.
i'm still having trouble
Let say for example i have JSON with the following format.
{ "sections":{ "2d4":{ "id":"2d4", "n":"MY_SECTION", "ls":{ "0":{ "n":"v1" }, "1":{ "n":"v2" } }, "ix":107 } }, "mb_index":528 }
foreach ($x as $sections_info) {
foreach($sections_info["sections"] as $k => $v){
echo $k;
}
}
PHP Notice: Undefined index: sections in jsonmachine.php on line 24
Is there a way to Return it as an Array instead of an object directly? I try converting it after reading it. The data malformed.
Can you please post your question to Github Discussions to Q&A category? New topic - Q&A. Let's continue there. Thank you.
This will make it more predictable as
json_decode
works the same way. The only thing needed to do is to change the line with default instantiation ofExtJsonDecoder
inParser
.Huge BC break - will wait to version 1.0
Done in master
branch.