Skip to content
Advertisement

How can I add JSON items to a PHP string?

I’m trying to concatenate some JSON items into a string in PHP. This is my JSON list:

{
   "StatusCode":200,
   "UserAccess":"True",
   "Username":"James"
}

I want to concatenate items from my JSON list to a string. This is what I tried to do:

echo "Your username is $jsonItems['Username']"

This returned an error stating: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in file

What can I do to fix this issue?

Advertisement

Answer

The value that you’re accessing is an object. This is seen by the curly braces. You can concatenate the string as seen below.

echo "Your username is " . $jsonItems->Username

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement