[英]converting json to xml or not
I am using PHP files to provide my iOS app with JSON data. 我正在使用PHP文件为我的iOS应用提供JSON数据。 Now I am developing an Android app that should use the same PHP files to parse MySQL objects. 现在,我正在开发一个Android应用程序,该应用程序应使用相同的PHP文件来解析MySQL对象。 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. 我现在遇到的问题是,我正在按照互联网教程创建Android应用,并且它需要XML格式的MySQL对象。
I kindly request your advice to continue working with my Android app. 谨请您提出建议,以继续使用我的Android应用。
As example, here is my PHP to provide JSON objects to my iOS app: 例如,这是我的PHP为我的iOS应用提供JSON对象:
<?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
. 您绝对不应将数据格式从JSON
更改为XML
。 Using XML
as data format in mobile badly affects performance when it comes to speed with which data are sent. 在移动数据发送速度方面,在移动设备中使用XML
作为数据格式会严重影响性能。
Instead of changing data format use external lib for JSON
parsing such as gson
. 不用更改数据格式,而使用外部库进行JSON
解析,例如gson
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.