Convert "Java source code characters" in JSON string using PHP - php

I have a JSON string that contains Dal\u00e9. When I use json_decode on the JSON, it is converted to Dalé, however the original string that the JSON is from is Dalé. Why is this not converted properly?
I have found that "\u00E9" is the C/C++/Java source code encoding for é. However, to me this doesn't answer why this is going wrong.
Example of incorrect PHP output:
<?php
$opts = array('http'=>array('ignore_errors' => true));
$context = stream_context_create($opts);
$jsonurl = "http://api.kivaws.org/v1/loans/552804.json";
$json = file_get_contents($jsonurl, false, $context);
$json_output = array(json_decode($json));
$json_error = $json_output[0]->error;
$json_message = $json_error->message;
foreach ($json_output[0]->{'loans'} as $loan) {
echo 'Name: '.$loan->{'name'};
}
?>

You need to tell the web browser what encoding you are giving it.
<?php
header('content-type: text/plain; charset=utf-8');
var_dump(json_decode($jsonStr));

if you are using php 5.4 you may use the function options of json_encode() like this :-
echo $b=json_encode('Dalé',JSON_UNESCAPED_UNICODE);
echo json_decode($b);

Related

Decode JSON to PHP

I have a .txt file called 'test.txt' that is a JSON array like this:
[{"email":"chrono#gmail.com","createdate":"2016-03-23","source":"email"}]
I'm trying to use PHP to decode this JSON array so I can send my information over to my e-mail database for capture. I've created a PHP file with this code:
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
echo $json;
?>
For some reason, it's not echoing the decoded JSON when I visit my php page. Is there a reason for this and can anyone shed some light? Thanks!
You use echo to print scalar variables like
$x = 'Fred';
echo $x;
To print an array or object you use print_r() or var_dump()
$array = [1,2,3,4];
print_r($array);
As json_decode() takes a JSON string and converts it to a PHP array or object use print_r() for example.
Also if the json_decode() fails for any reason there is a function provided to print the error message.
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo json_last_error_msg();
exit;
}
You'll need to split that json string into two separate json strings (judging by the pastebin you've provided). Look for "][", break there, and try with any of the parts you end up with:
$tmp = explode('][', $json_string);
if (!count($tmp)) {
$json = json_decode($json_string);
var_dump($json);
} else {
foreach ($tmp as $json_part) {
$json = json_decode('['.rtrim(ltrim($json_string, '['), ']').']');
var_dump($json);
}
}

Strip curly bracket from an output in PHP

My php code <?php echo $art->capt; ?> gives the following output:
{"path":"location\/file.png"}
However, I want to show only location/file.png as the output and remove all the junk.
How should I proceed?
That's json. you need to decode it.
<?php
$str = $art->$capt; //'{"path":"location\/file.png"}';
$json = json_decode($str, true);
$path = $json['path'];
echo($path);
?>
That is actually JSON, you can use json_decode to turn it into an object (or array if you pass true in the second argument of json_decode). Get the HTML output. If the HTML isn't already in a variable use output buffering.
ob_start();
/*html is output here*/
$json = ob_get_contents();
$json = json_decode($json, true);
$path = $json['path'];
print_r($path);
//output should be location/file.png

Get JSON object from URL Difficulties

I'm having difficulties grabbing any of the JSON information from this URL.
I've tried other JSON snippets and they seem to work so I'm not sure if it's the way that the URL is structured or something.
Basic example below.
<?php
$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);
echo "Body: " . $obj->Body;
?>
The link provided starts with
{ data :
which is valid javascript but invalid json. You can test it on http://jsonlint.com. To fix this we can replace the data with "data" :
$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) { //check if there was an error decoding json
$json = '{ "data" :'. substr(trim($json), 8); // replace the first 8-1 characters with { "data" :
$obj = json_decode($json);
}
print_r($obj->data); //show contents of data
Please note that this fix is dependent on the data source e.g. if they change data to dataset. The correct measure would be to ask the developers to fix their json implementation.

HOW TO PHP JSON DATA convert PHP Treeview

Hi Guys I have a JSON data I need to convert this data Treeview a json data url: http://torrent2dl.ml/json.php
recovered state = http://torrent2dl.ml/json.php?tree
I tried to do http://torrent2dl.ml/hedef.php
how to convert this data a php function or code ?
json_decode($jsonObject, true);
Use json_decode() :
<?php
$url = 'http://torrent2dl.ml/json.php';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
Source : https://stackoverflow.com/a/8344667/4652564

Google Translate API and Character Encoding

I am using the below PHP code with Google's Translate API and I have read that json_encode requires UTF-8 input, so was wondering how do I know if Google is returning UTF-8 encoded characters to me?
// URL Encode string
$str = urlencode($str);
// Make request
$response = file_get_contents('https://www.googleapis.com/language/translate/v2?key=' . GTRAN_KEY . '&target=es&source=en&q=' . $str);
// Decode json response to array
$json = json_decode($response,true);
if json_decode fails (for any reason, including charset-problems) it will return null, so you could check for that. to specifically chck the encoding, you could use mb_detect_encoding.
if(!mb_detect_encoding($response, 'UTF-8', true)){
// error: no utf-8
}else{
$json = json_decode($response,true);
if($json === null){
// error: json_decode failed (or google returned 'null')
}else{
// ok, do great stuff here
}
}

Categories