user3776241
user3776241

Reputation: 543

How do I keep json_decode from turning true and false into 0's and 1's?

I have this JSON string from reddit: http://www.reddit.com/.json. I am trying to get the "likes" field from this.

My problem is reddit uses "false" for the likes field, and "0" / null when there is nothing liked or unliked. When I use json_decode, it takes the "likes" field and changes the false to null if it is not liked. This causes me an issue, because now I can't tell if the post is disliked by the user or neutral.

I thought of replacing all of the "false" in the JSON string to "dislikes", but this doesn't work because if there is "false" in the post text, then that will be replaced as well.

                // what i have right now
                $response = str_replace("false", "\"dislikes\"", $output); 
                $out = json_decode($response, true);

How do I keep json_decode from changing "false" to 0?

Upvotes: 0

Views: 1057

Answers (1)

0yeoj
0yeoj

Reputation: 4550

You cannot change "false" to "dislikes"..
The reason is in your json it is false not "false" meaning the false in your json is in boolean type..

if you really want to change it modify the original array that was json_encoded:

sample:

// assuming this is your original array

$orginal_array = array("your_key" => false);

// change it to

$new_array = array("your_key" => "false");

Upvotes: 1

Related Questions