PHP json decoder string - php

anyone can help to decode this?
var_dump
sring(56441) "{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}"
I need to get "data" from this code.

<?php
$data='{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$data=json_decode($data,true);
echo '<pre>';
print_r($data['success']['data']);
It's a simple json that you need to make it an array and access it as a normal array. The output is the data field you asked:
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA

Well, you only need to json_decode() that string and you have your result:
<?php
$input = '{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$output = json_decode($input);
print_r($output->success->data);
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA

Related

PHP Using ($_GET['url']) and then displaying that json data?

<?php
function get_stuff()
{
($_GET['http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****']);
echo $_GET;
}
get_stuff();
?> /* Yes, Pointless closing tag, I'm cool like that. */
For some reason when I run this code the output I'm getting is "Array" and I can't figure out why? I know it is an array that I'm getting from the URL but I thought it would just print in whatever format its in? Am I missing something?
Thanks In advance!
You cannot pass website URL to $_GET.
You should use file_get_contents()
$content = file_get_contents('http://YOUR_URL');
If you have valid json then you can convert it to an array.
$data = json_decode($content, true);
then print it,
echo '<pre>'; print_r($data);
The Message of OP when I run this code the output I'm getting is
"Array".
You need to use file_get_contents to read a file from a remote server. If your file response an array then you have to use print_r or var_export or var_dump. Or if your file response is a string(json) then you need to store it in a variable and apply a decode method.
function get_stuff(){
$response = file_get_contents('http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****');
print_r($response); // if array
$arr = json_decode($response); // decode of json
print_r($arr);
}
get_stuff();
I think you will understand what i mean. Let me know if you are useful or need some help.

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 ....

How to get a Particular field from an object array in a text file

I am trying to get only the refresh_token field from the text file using file_get _contents. please anyone solve this.
{"access_token":"XXXX","token_type":"bearer","expires_in":3600,"refresh_token":"XXXX"}
For json manipulation you should use json_decode
$str= file_get_contents(some.txt);
$array = json_decode($content);
echo $array->access_token;
Thats a json string
$content = file_get_contents([...]);
$arr = json_decode($content);
echo $arr->access_token;

How to parse JSON string in php

I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
As everyone said, and you did - use json_decode.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var) and it will print type of variable and its content.
It is very easy:
$Arr = json_decode($JSON, true);
json_decode() will give you nested PHP types you can then descend to retrieve your data.
use json_decode($json_response,true) to convert json to Array
Guess what you are looking for is json_decode()
Check out http://php.net/manual/en/function.json-decode.php for the inner workings

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