0

I'm trying to read this in Objective-C but I keep getting errors.

When I run the json through a validator it tells me it is ok. But json_decode gets a null value.

What am I missing?

$arrTest = array("key" => "This is a string");
echo json_encode($arrTest);

$ob =  json_decode($arrTest);

if ($ob === NULL) { 
    print "\nDang it";
}
1
  • $arrTest is an array, not a string. You can't decode an array. Commented Apr 7, 2014 at 15:46

3 Answers 3

6

json_encode returns a new string, so you have to save that to a variable if you want to decode it later:

$arrTest = array("key" => "This is a string");
$jsonString = json_encode($arrTest);
echo $jsonString;

$ob =  json_decode($jsonString);

if ($ob === NULL) { 
    print "\nDang it";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone, it's been a long time since I've done any web front end work.
1

You're trying to decode the array, not the json string

Comments

1

You are decoding the array not the encoded string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.