I am using html_entity_decode($string) to decode html special characters such as ä=ä. I am then using json_encode() to create a json string that I use for an Android application. My problem is that i get an output of \u00e4 instead of 'ä'. I know that json_encode() only works with UTF-8 encoded strings, but when I run mb_detect_encding($myString) on my values it returns "UTF-8". It does't help to run ut8_encode() on the values. Here is my code:
$newsList = array();
while($row = $news->fetch_object()){
$tmpNews = new News();
$tmpNews->imgId = $row->image_id;
$tmpNews->author = html_entity_decode($row->author);
$tmpNews->subject = $row->subject;
$tmpNews->msg = $row->msg;
$tmpNews->newsmsg = $row->newsmsg;
$tmpNews->date = $row->wdate;
array_push($newsList, $tmpNews);
$tmpNews = null;
}
$json = array();
foreach($newsList as $news){
array_push($json, $news->getJson());
}
var_dump($json);
echo json_encode($json);
When i do the var_dump($json) my special characters shows as normal.
Related
I am trying to add JSON generated by a PHP script to VB.net data table but my JSON is not properly formatted (missing double quotes).
PHP:
header('Content-type: text/html; charset=utf8');
$conn = mysqli_connect();
$query = mysqli_query($conn, "SELECT * FROM installations where hwid = '".$_GET["hwid"]."'");
while($row = mysqli_fetch_assoc($query)) {
$data[] = $row;
}
echo htmlspecialchars(json_encode($data), ENT_QUOTES, 'UTF-8');
json_last_error_msg();
?>
Generates this JSON which is missing additional double quotes:
[{"idinstallations":"9","hwid":"74D435E5185A","naziv_racunala":"DESKTOP-0U5","naziv_instalacije":"prip","adresa_instalacije":"Vel","ukupno_artikala":"152","ukupno_dokumenata_d":"3","ukupno_dokumenata_arh":"0","ukupno":"20.50","prva_instalacija":"2019-06-14 05:25:45","licenca":"0","Opis_opa":"beb tipo","lastContact":"2019-06-30 14:05:43","version":"1.0.0.4\/a","dnevnik_summ":"20,50","arhiva_summ":"0","mod":"1","sync":"0","syncing":"","toMail":"xxx.ttt#gmail.com","licExpire":null,"teamviewer":""}]
This is my VB.net code which I am using to set JSON to datatable:
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString(http://www.something.com)
Dim json As String = result
Dim table As DataTable = JsonConvert.DeserializeObject(Of DataTable)(json)
I'm trying to convert a combined string into a ASCII Byte Array to pass it into a server as an http header. Been trying numerous ways like unpack, splitting strings and doing a loop to convert each. But the server I am passing the converted string still ignores it. Not so much of a support from the API I'm using so maybe anyone here can help if I'm doing anything wrong.
$billerId = '9999986379225246';
$authToken = '16dfe8d7-889b-4380-925f-9c2c6ea4d930';
$auth = $billerId . ':' . $authToken;
//this results in error
$auth_key_byte_array = unpack("H*",$auth);
//this also results in error
$auth_key_byte_array = hash_hmac("sha256", $auth, false);
//even tried a loop function
function create_byte_array($string){
$array = array();
foreach(str_split($string) as $char){
array_push($array, sprintf("%02X", ord($char)));
}
return implode('', $array);
}
$auth_key_byte_array = create_byte_array($auth);
I've got a really weird problem and I can't figure out why.
The situation is quite simple. My Android app uploads JSON data to a php script on my server. Right now I am trying to parse the data.
This is the JSON-Array passed to the script (via httpPost.setEntity ()):
[{"friends_with_accepted":"false","friends_with_synced":"false","friends_with_second_id":"5","friends_with_first_id":"6"}]
This is the php script:
<?php
// array for JSON response
$response = array();
$json = file_get_contents ('php://input');
$jsonArray = json_decode ($json, true);
foreach ($jsonArray as $jsonObject) {
$firstId = $jsonObject['friends_with_first_id'];
$accepted = $jsonObject ['friends_with_accepted'];
$secondId = $jsonObject ['friends_with_second_id'];
$synced = $jsonObject ['friends_with_synced'];
echo "accepted: ".$accepted."synced: ".$synced;
} ?>
And this is the response I get from the script:
accepted: synced: false
Why is the "synced" property correctly passed, but not the "accepted" property??
I can't see the difference. Btw, firstId and secondId are parsed correctly as well.
Okay, i just found the problem:
Instead of
$accepted = $jsonObject ['friends_with_accepted'];
I deleted the space between jsonObject and the bracket
$accepted = $jsonObject['friends_with_accepted'];
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);
I have json string, which is produced by server.php
[{"attr":{"id":"node_7","rel":"default"},"data":"doc.html","state":""},
{"attr":{"id":"node_8","rel":"folder"},"data":"New node","state":"closed"},
{"attr":{"id":"node_9","rel":"folder"},"data":"New node","state":""}]
How do I remove a full string that contains the value rel=default
This is the code I have for server.php.
require_once("config.php");
$jstree = new json_tree();
echo $jstree->{$_REQUEST["operation"]}($_REQUEST);
die();
Using PHP:
// convert json string to array
$json = json_decode($json_string);
// filter out items
$json = array_filter($json, function($item)
{
return $item->attr->rel != "default";
});
// convert back to string
$json_string = json_encode($json);
You can use data.items.splice(X, Y); to remove an element ;)