json to php array cdnjs - php

I am trying to convert cdnjs api JSON result to php array from this link. Here is my code:
<pre>
<?php
$cdnLinks = file_get_contents('http://api.cdnjs.com/libraries');
$cdnLinks = json_encode($cdnLinks);
$j = json_decode($cdnLinks);
print_r($j);
?>
</pre>
What am I missing? Thanks

This is what you are doing:
Download a string of JSON
Encode that string into JSON (you now have a JSON string containing a JSON object)
Decode that JSON back to a string of JSON
Print the string
You need to skip step 2.

Skip step 2 because the output is already a json string.
To decode a json into an array you also need to use second parameter as true json_decode($jsonObj, true).
Thanks

Related

How to convert the encoded value into another format

YTo2OntpOjA7czo0OiIyMDY3IjtpOjE7czo0OiIyMDY4IjtpOjI7czo0OiIyMDY5IjtpOjM7czo0OiIyMDcwIjtpOjQ7czo0OiIyMDcxIjtpOjU7czo0OiIyMDcyIjt9
The above is my encoded data.
When I try to decode it I am getting the output in this format:
a:6:{i:0;s:4:"2067";i:1;s:4:"2068";i:2;s:4:"2069";i:3;s:4:"2070";i:4;s:4:"2071";i:5;s:4:"2072";}
How can I convert it into this
["2067","2068","2069","2070","2071","2072"]
or this
(2067,2068,2069,2070,2071,2072)
?
The data in your second block is serialised in PHP's object serialisation format. You can use the unserialize function to turn it back into a PHP variable:
$data = 'a:6:{i:0;s:4:"2067";i:1;s:4:"2068";i:2;s:4:"2069";i:3;s:4:"2070";i:4;s:4:"2071";i:5;s:4:"2072";}';
$obj = unserialize($data);
echo json_encode($obj);
This outputs
["2067","2068","2069","2070","2071","2072"]
which is an array in the JSON format you requested.
Live demo: http://sandbox.onlinephpfunctions.com/code/6e9841c72950d44abd0ec6d45e2815cdcc89f42d

How to remove slashes from array key using PHP?

I am getting some slashes inside my JSON array key using PHP. I am providing my code below.
$result[] = $fcm->send_fcm_notify($device_id, $message);
echo json_encode($result);
The output of the above code is below:
{\"multicast_id\":7339396188598826217,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482327583160431%2d865361f9fd7ecd\"}]}"]
I need to remove the slashes, because I need to check success==1.
Use stripslashes() first on response then do json_decode()
Try
$json = json_decode(stripslashes('{\"multicast_id\":7339396188598826217,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482327583160431%2d865361f9fd7ecd\"}]}'));
if( $json->success == '1' )
{
echo "I got it";
}
Output
You can use json_decode() to conrect received json string into multidimensional array then you can use json_encode() to print that array in json format without any slashes.
But make sure your received json string is valid as i can see the json code is not valid this will throw error when you use json_decode.
Your json string is wrong so json_encode will not work i have removed extra ] at the end of string so that json can parse without any error.
<?php
$json_code = "{\"multicast_id\":7339396188598826217,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1482327583160431%2d865361f9fd7ecd\"}]}";
$result = json_decode($json_code,true);
//for json output use json_encode()
echo json_encode($code);
?>

How to insert an array in a json object in PHP

I have a string on my databas, that I'm trying to insert into a json object as an Array, not a string..:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
$jsonObject = array('numbers' => $arrayInString);
$json = json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
echo $json;
When I execute this.. my Json object is..
numbers: "[2,3,5,5,6]";
and not
numbers: [2,3,5,5,6];
Like I originally wanted.. can't get this to work, can anyone help?
Like was said you need to decode the passed data from the database and then build your array output. Something like this:
$arrayInString = "[2,3,5,5,6]"; // this comes from the database
// decode the JSON from the database as we build the array that will be converted back to json
$jsonObject = array('numbers' => json_decode($arrayInString));
echo json_encode($jsonObject, JSON_UNESCAPED_SLASHES);
The slightly modified code above outputs:
{"numbers":[2,3,5,5,6]}
You need to json_decode your $arrayInString variable before you add it to the associative array.

Query and decode json form mysql database

I have this JSON encoded code in my mysql database:
{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}
And i want to query and decode it to echo it at my website.
You have to use a php "json_decode()" function to decode a json encoded data.
Basically json_decode() function converts JSON data to a PHP array.
Syntax: json_decode( data, dataTypeBoolean, depth, options )
data : - The json data that you want to decode in PHP.
dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". Both data types can be accessed like an array and use array based PHP loops for parsing.
depth :- Optional recursion limit. Use an integer as the value for this parameter.
options :- Optional JSON_BIGINT_AS_STRING parameter.
Now Comes to your Code
$json_string = '{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}';
Assign a valid json data to a variable $json_string within single quot's ('') as
json string already have double quots.
// here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.
/ just can check here your encoded array data.
// echo '<pre>';
// print_r($json_decoded_data);
// loop to extract data from an array
foreach ($json_decoded_data as $key => $value) {
echo "$key <br/>";
foreach($value as $k=>$data)
{
echo "$k | $data <br/>";
}
}

Parse mtgapi's response into an associative array

I would like to have the data returned from mtgapi to be placed into an array of associative keys and values.
Sample output can be found at http://mtgapi.com/docs.
As the output is returned as a string, I pull it into PHP as a string using file_get_contents($url);
Edit:
I did not know the return is a JSON object! Simple!
It's returning a JSON object. You should use json_decode.
Try:
$json = file_get_contents($url);
var_dump(json_decode($json, true));
source

Categories