Parse mtgapi's response into an associative array - php

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

Related

PHP parse multidimensional array with json

I'm trying to parse a json string extracted from a MySQL database. The json string contains a two-dimensional array, but json_decode returns null and json_last_error returns 0 which I assume means no error occurred.
$result is the json string
json_decode($result, true);
The string:
[
["17544500374","17544500489","17544500571","17544500587","17544500528"],
["17544500651","17544500432","17544500673","17544500452","17544500362"],
["17544500454","17544500457","17544500523","17544500441"],
["17544500547","17544500463","17544500535","17544500676"],
["17544500548","17544500581","17544500584","17544500382"],
["17544500593","17544500364","17544500660","17544500595"],
["17544500635","17544500647","17544500529","17544500670"]
]
You do not have a key for your values. That means, your second parameter is not correct, because the function is unable to create an associative array.
This works fine on my mashine:
json_decode($result);
The $result must be a string:
$result = '[["17544500374","17544500489","17544500571","17544500587","17544500528"],["17544500651","17544500432","17544500673","17544500452","17544500362"],["17544500454","17544500457","17544500523","17544500441"],["17544500547","17544500463","17544500535","17544500676"],["17544500548","17544500581","17544500584","17544500382"],["17544500593","17544500364","17544500660","17544500595"],["17544500635","17544500647","17544500529","17544500670"]]'

json_decode returns json string not an array

<?php
$json=file_get_contents('php://input',true);
$data = json_decode($json, true);
print_r($data);
?>
Output given is {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}
Json Data posted is:
{"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}
Writing the json data in a variable and passing it to json_decode works but posting the same from the "php://input" returns a JSON data instead of associative array.
It looks like #tkausl is correct. The JSON you're receiving has been double-encoded. Since it's double-encoded, a temporary solution would be to double-decode it.
$data = json_decode(json_decode($json), true);
But the real solution is to figure out why it's like that to begin with and fix it (if it's yours to fix).

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.

How do I "trim" this JSON with PHP?

I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....

Reading multiple json values with php

I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for

Categories