Comments (12)
I wouldn't say changing, just add an option to allow also arrays.
I will try, if I get some time.
from hjson-php.
I think I found a solution.
This seems to work:
$parser = new HJSONParser();
$jsonData = $parser->parse($json);
$arrays = json_decode(json_encode($jsonData), true);
Can you think of a caveat in this case?
from hjson-php.
parse()
already returns an array:
$hjsonText="[1,2,3]";
$parser = new HJSONParser();
$v = $parser->parse($hjsonText);
print_r($v);
prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
from hjson-php.
Not if you have something more complex, like this:
{"some_key":"a","some_other_key":"b","settings":{"required":false}}
This will get parsed to this (output from var_dump):
object(stdClass)#809 (3) {
["some_key"]=>
string(1) "a"
["some_other_key"]=>
string(1) "b"
["settings"]=>
object(stdClass)#813 (1) {
["required"]=>
bool(false)
}
}
from hjson-php.
Ah, I see. I'm not a PHP dev, this repo was contributed by @iainhallam and @jawb. Is the idiomatic php way to use arrays instead of objects?
from hjson-php.
Yes, usually we work with arrays. Also, I think the arguments should emulate the ones of json_decode.
from hjson-php.
@iainhallam Would you be OK with changing the API to arrays?
@cdarken Would you be willing to do a PR or do you know someone who would?
from hjson-php.
Hey, this works fine for me:
$hjsonText="{"some_key":"a","some_other_key":"b","settings":{"required":false}}";
$parser = new HJSONParser();
$v = (array)$parser->parse($hjsonText);
print_r($v);
from hjson-php.
@4cc355-d3n13d it doesn't, the "setting" key has a stdClass instance as value, it should be array
from hjson-php.
also see #7
from hjson-php.
@cdarken parse the array recursively
from hjson-php.
This is now implemented, thanks @nichtich !
from hjson-php.
Related Issues (18)
- Found ':' but no key name (for an empty key name use quotes)
- { :0 } causes Fatal Error HOT 4
- UTF8 HOT 4
- String fails in PHP but not in js HOT 3
- PHP 5.6 and 7.0 EOL HOT 11
- can't stringify assoc arrays HOT 1
- Very slow on large data HOT 7
- UTF-8 in multiline strings bug HOT 1
- Incorrect stringification of falsy strings HOT 1
- Travis fails for PHP 5.5
- Disable Hash (#) comments
- Multiline strings where a line is just 0 gets truncated HOT 1
- Parse failed with "0" char in multiline string HOT 1
- Cirillic chars in comments breaks script HOT 7
- Contributor Wanted! HOT 6
- Support for single quoted strings HOT 1
- Can't install with composer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hjson-php.