سؤال

This is my JSON

$result=[{CFirst: "test3"},{CLast: "test3"},{CEmail: "test2@gmail.com"}]

I am getting this as a result of an API request. I guessed it was json, so tried to json_decode(), but it didn't work and json_last_error() returned code 4.

Thanks for help

هل كانت مفيدة؟

المحلول

Your JSON is in bad format , you need to enclose them under double quotes like this.

[{"CFirst": "test3"},{"CLast": "test3"},{"CEmail": "test2@gmail.com"}]

The CFirst,CLast & CEmail have been wrapped around double quotes for your information.

The code..

<?php
$json='[{"CFirst": "test3"},{"CLast": "test3"},{"CEmail": "test2@gmail.com"}]';
print_r(json_decode($json,true));

Demo

EDIT :

<?php
$result='[{CFirst: "test3"},{CLast: "test3"},{CEmail: "test2@gmail.com"}]';
$result=str_replace(array('{',':'),array('{"','":'),$result); //<--- Add this
print_r(json_decode($result,true));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top