In PHP, I have a JSON object like so (even here not sure if its correctly formatted and if I'm passing it correctly):
$someJSONObject = '{token:"randomtoken",signature:"signature"};
and encode it before passing the response:
$response['code'] = 1;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = $someJSONObject;
my_response($_GET['format'], $response); //json_encode($response)
then in my JS I receive it in string format like this:
{ code:1,
status:200,
data: '"{token:\\"randomtoken\\", signature:\\"signature\\"}"' }
I parse it into an object:
phpObj = JSON.parse(body);
so I can access 'data':
dataObj = phpObj.data;
which gives me the result as a string:
{token:\"randomtoken\", signature:\"signature\"} //console.log(dataObj)
it's here where I lose my way and not able to access 'token' getting an undefined:
console.log('token: ' + dataObj.token) //token:undefined
I realize I'm trying to dot into a string, so I must be doing something wrong at this last step. However, even if I try to use JSON.parse or JSON.stringify it still doesn't seem to help and gives me an 'undefined'.
dataObj = JSON.parse(phpObj.data);
console.log(typeof(dataObj); //string
console.log(dataObj); //"\"{token:\\\"randomtoken\\\", signature:\\\"signature\\\"}\""
console.log(dataObj.token); //token: undefined
or
dataObj = JSON.stringify(phpObj.data);
console.log(typeof(dataObj)); //string
console.log(dataObj); //"\"{token:\\\"randomtoken\\\", signature:\\\"signature\\\"}\""
console.log(dataObj.token); //token: undefined
Any help/feedback would be appreciated.
In PHP, I have a JSON object like so (even here not sure if its correctly formatted and if I'm passing it correctly):
$someJSONObject = '{token:"randomtoken",signature:"signature"};
and encode it before passing the response:
$response['code'] = 1;
$response['status'] = $api_response_code[ $response['code'] ]['HTTP Response'];
$response['data'] = $someJSONObject;
my_response($_GET['format'], $response); //json_encode($response)
then in my JS I receive it in string format like this:
{ code:1,
status:200,
data: '"{token:\\"randomtoken\\", signature:\\"signature\\"}"' }
I parse it into an object:
phpObj = JSON.parse(body);
so I can access 'data':
dataObj = phpObj.data;
which gives me the result as a string:
{token:\"randomtoken\", signature:\"signature\"} //console.log(dataObj)
it's here where I lose my way and not able to access 'token' getting an undefined:
console.log('token: ' + dataObj.token) //token:undefined
I realize I'm trying to dot into a string, so I must be doing something wrong at this last step. However, even if I try to use JSON.parse or JSON.stringify it still doesn't seem to help and gives me an 'undefined'.
dataObj = JSON.parse(phpObj.data);
console.log(typeof(dataObj); //string
console.log(dataObj); //"\"{token:\\\"randomtoken\\\", signature:\\\"signature\\\"}\""
console.log(dataObj.token); //token: undefined
or
dataObj = JSON.stringify(phpObj.data);
console.log(typeof(dataObj)); //string
console.log(dataObj); //"\"{token:\\\"randomtoken\\\", signature:\\\"signature\\\"}\""
console.log(dataObj.token); //token: undefined
Any help/feedback would be appreciated.
Share Improve this question asked Nov 7, 2013 at 15:38 0--0-- 2211 gold badge6 silver badges16 bronze badges3 Answers
Reset to default 4You should NOT be embedding json-in-json. It's rather pointless. Deal with a purely NATIVE data structure, e.g.
$response = array(
'code' => 1,
'status' => xxx,
'data' => array
'token' => 'randomtoken',
etc...
)
);
And then encode that:
echo json_encode($response);
The JS side will decode that back into a native JS structure, and then you have, simply:
alert(response.data.token);
with no extra decoding steps, no worries about escaping quotes, etc...
First, this is not valid JSON:
$someJSONObject = '{token:"randomtoken",signature:"signature"};
You need to enclose the keys in "
. Better still, let PHP do it:
$someJSONObject = json_encode(array(
'token' => 'randomtoken',
'signature' = 'signature'
));
The really odd thing you're doing, however, is attempting to include a string of JSON in another string of JSON. This is, to put it mildly, confusing.
Instead, pass the data to my_response
as an array, and let json_encode
encode the whole lot.
$someJSONObject = array(
'token' => 'randomtoken',
'signature' = 'signature'
);
my_response($_GET['format'], $response);
In your Javascript, you should then be able to do phpObj.data.token
.
I have a code similar but, my json looks like this
{ "token" [ "token": randomtoken, "signature": signature] }
then I manipulate like this:
var datos = JSON.parse(JSON);
for(var something in datos){
var objeto = datos[something];
alert('token: ' + objeto.token);
}
sorry my bad english