简体   繁体   English

是否将json转换为xml

[英]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应用。

  1. Should I change my app to be able to use the same PHP files as the iOS app? 我应该更改我的应用程序使其能够使用与iOS应用程序相同的PHP文件吗?
  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? 还是有一种简单的方法,可以在从PHP文件中进行复制然后更新它们以将输出类型从JSON更改为XML之后,让我使用它们以供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.

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