我正在生成一个Json文件,每个元素的分隔符都是
[
我会有
{
字符作为分隔符。不确定但是我没有使用Angularjs应用程序将结果收回到Ionic中,当我使用{
对结果进行编码时,我确实收到了结果。我这样做:$ result [] = json_encode($ JsonElement); :
$result = array();
$JsonElement = array();
while ($row = oci_fetch_array($stid)) {
foreach ($row as $item) {
$JsonElement[] = $item;
}
$result[] = json_encode($JsonElement);
$JsonElement = null;
$JsonElement = array();
}
echo json_encode($result);
输出
[
"["123","1"]",
"["456","2"]",
"["789","3"]"
]
我希望json使用这种格式:
[
{"123","1"},
{"456","2"},
{"789","3"}
]
答案 0 :(得分:1)
我从PHP到Angular的JSON数据表现出奇怪的表现。如果您使用$http
服务发送请求,则需要在其中包含标题字段:
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
对于PHP文件,您需要正确的标题:
header("Content-Type: application/json"); //Set header for outputing the JSON information
http_response_code(200);
echo json_encode($result, JSON_PRETTY_PRINT); //After you're done adding in all the necessary information from the while loop
你不应该自己需要"硬核"(不确定你的意思)JSON格式,因为json_encode
会照顾你。同时从您的循环中删除json_encode
,您不需要它两次:
while ($row = oci_fetch_array($stid)) {
foreach ($row as $item) {
$JsonElement[] = $item;
}
$result[] = $JsonElement;
$JsonElement = null;
$JsonElement = array();
}
我对你的循环结构有点困惑,也许我错了,但我认为它应该是这样的:
while ($row = oci_fetch_array($stid)) {
$result[] = $row;
}
我认为你正在做额外的事情以使JSON
格式正确吗?