我尝试使用json_encoding大约两个小时,但我没有按要求获得输出.实际上这是移动应用程序开发人员要求的格式,我将在这里解释.下面的代码是我尝试过的:
include_once("class_connection.php");
//Getting the Parent Category
$sqlStr = mysql_query("select catname , id from `category` where `parentid`='0'");
$jsonArray = array();
while ($fetchStr = mysql_fetch_assoc($sqlStr)) {
$jsonArray[] = array("ParentCategory" => $fetchStr["catname"]);
$id = $fetchStr['id'];
//Getting child categories from the above parent
$sqlChildStr = mysql_query("SELECT catname,id,parentid FROM `category` where `parentid`='$id'");
while ($fetchchildStr = mysql_fetch_assoc($sqlChildStr)) {
$jsonArray[] = array("ChildCategory" => $fetchchildStr["catname"]);
}
}
echo json_encode(array("JsonOutput" => $jsonArray)) . "<br />";
Run Code Online (Sandbox Code Playgroud)
输出是:
"JsonOutput":[{"ParentCategory":"Animals"},{"ChildCategory":"Bear"},{"ChildCategory":"Deer"},{"ChildCategory":"Dolphins"},
{"ParentCategory":"Art"},{"ChildCategory":"Hand Painting"},{"ChildCategory":"Painting"},{"ChildCategory":"3D"},{"ChildCategory":"Abstract"}]}
Run Code Online (Sandbox Code Playgroud)
这里,在上面的输出中,父类别数组是空的,没有子类别数组.我想将所有子类别数组存储在其父类别数组中,最后我必须将父类和子类存储到JsonOutput数组中,所以我希望输出为
"JsonOutput":[{
"ParentCategory":"Animals" : [{
{"ChildCategory":"Bear"},{"ChildCategory":"Deer"},{"ChildCategory":"Dolphins"}
]}
"ParentCategory":"Arts" : [{ …Run Code Online (Sandbox Code Playgroud) 我有以下结果
Array (
[0] => stdClass Object ( [name] => Identification )
[1] => stdClass Object ( [name] => Assay )
[2] => stdClass Object ( [name] => pH(Acidity/Alkalinity))
[3] => stdClass Object ( [name] => Sterility )
)
Run Code Online (Sandbox Code Playgroud)
我想要的是使用逗号分隔对象数组值并返回为字符串,以便得到此结果:
Identification, Assay, ph(Acid/Alkalinity), Sterility
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容
$data=(array)$result;
$answer=implode(",",$data);
Run Code Online (Sandbox Code Playgroud)
这个回报:
Message: Object of class stdClass could not be converted to string
Run Code Online (Sandbox Code Playgroud)
如何才能实现这一目标?
我想将脚本添加到 jquery mobile 中的外部页面而不是 home 文件(调用 jquery mobile 的源代码)。这是我的两个文件的代码
index.php 里面的代码
<head>
<title>My Web Application</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0;" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="view/css/mobile.css" />
<link rel="stylesheet" href="view/css/reset.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript" src="view/js/light.js"></script>
</head>
<body>
<div data-role="page" class="page">
<div data-role="header" id="index-header" data-theme="b">
<div id="logo">
<a href="#"><img src="view/images/Milan.png" alt="logo" height="80"></a>
</div>
</div>
<div id="color-bar"></div>
<div data-role="content">
<ul data-role="listview" data-inset="true" data-theme="b">
<li><a href="#" title="theme"><img src="view/images/1.png" />Security</a></li>
<li><a href="light.php" title="calendar"><img src="view/images/2.png" />Info</a></li> …Run Code Online (Sandbox Code Playgroud)