Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse into array #5

Closed
cdarken opened this issue Aug 10, 2016 · 12 comments
Closed

Parse into array #5

cdarken opened this issue Aug 10, 2016 · 12 comments

Comments

@cdarken
Copy link

cdarken commented Aug 10, 2016

How can I get arrays instead of objects ?

@cdarken
Copy link
Author

cdarken commented Aug 10, 2016

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?

@laktak
Copy link
Member

laktak commented Aug 10, 2016

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
)

@cdarken
Copy link
Author

cdarken commented Aug 10, 2016

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

@laktak
Copy link
Member

laktak commented Aug 10, 2016

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?

@cdarken
Copy link
Author

cdarken commented Aug 10, 2016

Yes, usually we work with arrays. Also, I think the arguments should emulate the ones of json_decode.

@laktak
Copy link
Member

laktak commented Aug 10, 2016

@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?

@cdarken
Copy link
Author

cdarken commented Aug 10, 2016

I wouldn't say changing, just add an option to allow also arrays.
I will try, if I get some time.

@4cc355-d3n13d
Copy link

4cc355-d3n13d commented Jan 15, 2017

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

@cdarken
Copy link
Author

cdarken commented Jan 15, 2017

@4cc355-d3n13d it doesn't, the "setting" key has a stdClass instance as value, it should be array

@laktak
Copy link
Member

laktak commented Jan 17, 2017

also see #7

@4cc355-d3n13d
Copy link

@cdarken parse the array recursively

nichtich added a commit to nichtich/hjson-php that referenced this issue Jan 21, 2018
laktak added a commit that referenced this issue Jan 24, 2018
Support parsing into associative array (#5)
@laktak
Copy link
Member

laktak commented Jan 24, 2018

This is now implemented, thanks @nichtich !

@laktak laktak closed this as completed Jan 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants