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)
Related
**$sql = 'select * from dish_category';
$results =$conn->query($sql);
$spots = array(); //array to parse jason from
while($spot = $results->fetch_assoc()){
$spots[] = $spot;
}
foreach($spots as $key=>$value){
$newArrData[$key] = $spots[$key];
$newArrData[$key]['Cat_Image'] = base64_encode($spots[$key]['Cat_Image']);
}
header('Content-type: application/json');
echo json_encode($newArrData);**
Above is the PHP code, it works perfectly fine and return JSON string, now i am confused how to parse image(decode base64) in android?
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
You should do it one by one:
Get the JSON response as string in android.
Parse the JSON and get the base64 string only.
Convert the base64 string to byte array by decoding it.
Create a new ByteArrayInputStream using the byte array.
Use the input stream to create a Bitmap: BitmapFactory.decodeStream(inputStream)
I am trying to get JSON posted by an android application and decode it into an array to store it in my sqldb.
I have written the following php code. When I execute it, it displays [] (line 6).
<?php
include_once './db_functions.php';
//Create Object for DB_Functions class
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : '');
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
//Based on insertion, create JSON response
if($res){
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>
I am sending base64 encode image from Android to SQL server DB but image is not stored in the DB folder. The android step to convert the image into base64 string is mentioned below.
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
After converting image to base64 string using volley i am posting into SQL DB. Then using PHP decode base64 string and save the image path in the DB table. The path is stored but not the image in the folder. What is wrong about it.
PHP code used:
if($_SERVER['REQUEST_METHOD']=='POST'){
$FoodName = $_POST['FoodName'];
$FoodLocation = $_POST['FoodLocation'];
$ShopName = $_POST['ShopName'];
$Type = $_POST['Type'];
$image = $_POST['image'];
require_once('dbConnect.php');
$sql ="SELECT id FROM Volley ORDER BY id ASC";
$res = mysqli_query($con,$sql);
$id = 0;
while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}
$path = "images/New folder/$id.jpg";
$actualpath = "http://ccc.ccc.com/$path";
$sql = "INSERT INTO Volley (FoodName,FoodLocation,ShopName,Type,image) VALUES ('$FoodName','$FoodLocation','$ShopName','$Type','$actualpath')";
if(mysqli_query($con,$sql)){
file_put_contents('$path',base64_decode($image));
echo "Successfully posted";
}else{
echo "Could not post";
}
}else{
echo 'error';
}
BASE 64 from Android:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAGLAScDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD92vGf
jfwN8MPBvir4h/EvxVZeD/A/g7Q9S1zxV4q1vUjp3hrw34a05mAA1YHknbx1Ocg5OCfzZ8EfG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qr3O9/YS/0z/gpl/wWHvLz/SrrS779izQr
If your base64encoded string has data:image/jpeg;base64, appended infront of it, then you need to remove it before base64_decode
You can use something like this for it
str_replace("data:image/jpeg;base64,","",$image);
UPDATE
I tried to convert your string here But it seems to be corrupted. The base64 string is not proper.
This might happen, if there are new line characters in the code. So try replacing \n or any other white spaces from the string before decoding it. You can use str_replace("\n", "", $image) for that as well. Try and let me know
i want to use PHP for internet service in my IOS 7 application.
I've tried to generate an code for receiving objects from a Firebird database.
My Code bellow:
<?php
$host = 'xxx';
$username = 'xxx';
$password = 'xxx';
$conn = ibase_connect($host, $username, $password) or
die ("Verbindung fehlgeschlagen");
$arr = array();
$data = array();
$stmt = "select * from xxx";
$sth = ibase_query($conn, $stmt);
while ($row = ibase_fetch_object ($sth)) {
$data['ID'] = $row->ID;
$data['VON'] = $row->VON;
$data['AN'] = $row->AN;
$data['BETREFF'] = $row->BETREFF;
$data['DATUM'] = $row->DATUM;
$data['KOMMISSIONSNR'] = $row->KOMMISSIONSNR;
$data['NACHRICHT'] = $row->NACHRICHT;
$data['GELESEN'] = $row->GELESEN;
$data['GEANTWORTET'] = $row->GEANTWORTET;
$data['STATUS'] = $row->STATUS;
$data['AUFTRAG'] = $row->AUFTRAG;
$arr[] = array($data);
}
echo json_encode($arr);
?>
By calling the code in Safari-browser no values or arrays be returned.
If giving an Key like 1
(echo json_encode($arr[1]);) an array on the browser will be shown.
How can i show the whole array with all keys? I should add that i want to use the results from my ibase-query as NSDictionary in the application.
And which framework is the best for encoding the PHP-Array to IOS? I've tried to use JSONDictionaryExtensions, but don't know if it's the right for this Code.
i hope you can help me.
Sorry for my bad english.
I'm not sure why you are putting the $data array into another array. You can skip that step.
When you echo json_encode($data) you encode the array as JSON, so that is what your iOS application gets and what it needs to decode. Decoding JSON into an NSArray or NSDictionary has been asked many times here on StackOverflow, like this question.
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.