简体   繁体   中英

converting json to xml or not

I am using PHP files to provide my iOS app with JSON data. Now I am developing an Android app that should use the same PHP files to parse MySQL objects. The problem I have now is that I am creating the Android app following an internet tutorial and it needs the MySQL objects in XML format.

I kindly request your advice to continue working with my Android app.

  1. Should I change my app to be able to use the same PHP files as the iOS app?
  2. Or is there an easy way that after making a copy from the PHP files and then updating them to change the output type from JSON to XML would allow me to use them to be used by the Android app?

As example, here is my PHP to provide JSON objects to my iOS app:

<?php
$host = "localhost"; // host of MySQL server
$user = "hidden"; // MySQL user
$pwd = "hidden"; // MySQL user's password
$db = "hidden"; // database name

// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);
mysql_query("SET NAMES 'utf8'");
// Check connection
if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 

// query the application data
$sql = "SELECT * FROM tbempresas";
$result = mysqli_query($con, $sql);

// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $rows[] = $row; 
}

// close the database connection
mysqli_close($con);

// echo the application data in json format
echo json_encode($rows);
?>

您可以使用json_decode()PEAR :: XML_Serializer将输出类型转换为XML,然后可以在Android应用中使用它。

You definitely should not change the data format from JSON to XML . Using XML as data format in mobile badly affects performance when it comes to speed with which data are sent.

Instead of changing data format use external lib for JSON parsing such as gson .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM