0

I am extracting the values from JSON but keep getting an empty result when I echo the value

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';

$obj2 = json_decode($json, true);

$displayName = $obj2->Surname;

echo"$displayName";
4
  • 1
    What's that $xmlresponse? Commented Oct 23, 2013 at 21:55
  • 1
    Is $xmlresponse supposed to be $json ? And where did the $value come from ? Commented Oct 23, 2013 at 21:55
  • This calls for basic debugging. What do you get when you do a print_r($obj2);? Commented Oct 23, 2013 at 21:55
  • why are you decoding $xmlresponse instead of $json? Commented Oct 23, 2013 at 21:55

4 Answers 4

1

you have one object in other one in this json string

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';

$obj2 = json_decode($json);

print_r($obj2);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<?php
$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';
$obj2 = json_decode($json, true);
$displayName = $obj2[0][0]['Surname'];
echo "$displayName";
?>

Comments

0

This should be something like:

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address    Line 1":"RG412GX"}]]';

$obj2 = json_decode($json, true);

$displayName = $obj2->Surname;

echo"$displayName";

You are mixing up / making up variable names...

Comments

0

There are several issues with the code you provided. First, your json text is stored in $json, but you try to decode $xmlresponse. I guess that's just a copy/paste error, though. Second, you try to access the surname using the object syntax although you explicitly force json_decode to decode objects as associative arrays. Third, the json provided encodes an object in an array in an array. You ignore the nested structure of the response.

Try this:

$json='[[{"transTime":"2013-10-23 17:30:42","Forename":"Ian","Surname":"Graham","Address Line 1":"RG412GX"}]]';
$response = json_decode($json);
$displayName = $response[0][0]->Surname;

echo $displayName;

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.