您好我正在尝试使用php从我正在编码的Swift应用程序访问我的数据库。到目前为止,读表已经很好了,除了现在我正在尝试读取一个包含多行包含json的表。这一直在抛出错误,我似乎无法让最终输出与我想要的相同,或者任何与swift代码一起工作的东西。 json最初只输出为null。在研究如何解决这个问题后,我尝试了utf8_encode(),但这给了太多额外的字符,应用程序中的Swift代码无法理解它。当只输出其中一行时,它会很好,当我尝试将它们放在一个关联数组中时,将它们作为json放在它们出现为null时。
PHP代码:
$sql = "Select * FROM User WHERE Id = '".$UserId."' LIMIT 1";
mysql_select_db($database, $User);
$result = mysql_query($sql , $User) or die(mysql_error());
$FleetRaw = mysql_fetch_assoc($result);
$Fleet1 = $FleetRaw['Fleet1'];
$Fleet2 = $FleetRaw['Fleet2'];
$Fleet3 = $FleetRaw['Fleet3'];
$Fleet4 = $FleetRaw['Fleet4'];
$Fleet5 = $FleetRaw['Fleet5'];
$Fleet6 = $FleetRaw['Fleet6'];
$Fleets = array("1"=>$Fleet1,"2"=>$Fleet2,"3"=>$Fleet3,"4"=>$Fleet4,"5"=>$Fleet5,"6"=>$Fleet6);
//Output 1
echo $Fleets["1"]."<br><br><br>";
//Output 2
echo json_encode(utf8_encode($Fleets["1"]))."<br><br><br>";
//Output 3
echo json_encode($Fleets);
?>
输出:
Output 1:
{ “status” : 3, “game” : 0, “ships” : { "1" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 100 }, "3" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : -100 }, "2" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 0 }, "0" : { "level" : 0, "className" : "MotherShip", "posX" : 0, "health" : 100, "posY" : 0 } } }
Output 2:
"{\n\u0093status\u0094 : 3,\n\u0093game\u0094 : 0,\n\u0093ships\u0094 : {\n \"1\" : {\n \"level\" : 0,\n \"className\" : \"LighteningShip\",\n \"posX\" : 100,\n \"health\" : 50,\n \"posY\" : 100\n },\n \"3\" : {\n \"level\" : 0,\n \"className\" : \"LighteningShip\",\n \"posX\" : 100,\n \"health\" : 50,\n \"posY\" : -100\n },\n \"2\" : {\n \"level\" : 0,\n \"className\" : \"LighteningShip\",\n \"posX\" : 100,\n \"health\" : 50,\n \"posY\" : 0\n },\n \"0\" : {\n \"level\" : 0,\n \"className\" : \"MotherShip\",\n \"posX\" : 0,\n \"health\" : 100,\n \"posY\" : 0\n }\n}\n}"
Output 3:
{"1":null,"2":null,"3":null,"4":null,"5":null,"6":null}
输出1正是我想要的格式(Swift理解的格式),除了它只是六行中的一行(也是应用程序拒绝此表单,因为它在回显之前不是json_encode)。输出2是六行中的一行的示例,当使用utf8_encode()之前保存到数组中给出了许多额外的字符,但是当放入六个数组时它输出为非null。输出3是我想要最终输出的,只是没有null。
理想的情况是将输出1和3组合在一起,这样我就可以输出六个数组,看起来像输出1.此外,应用程序仅在我json_encode回显时才起作用。如果有人有可能做到这一点,请告诉我!!
谢谢!
最近的尝试,工作但数据加倍?
$Fleet1 = $FleetRaw['Fleet1'];
$Fleet2 = $FleetRaw['Fleet2'];
$Fleet3 = $FleetRaw['Fleet3'];
$Fleet4 = $FleetRaw['Fleet4'];
$Fleet5 = $FleetRaw['Fleet5'];
$Fleet6 = $FleetRaw['Fleet6'];
$Fleets = array("1"=>$Fleet1,"2"=>$Fleet2,"3"=>$Fleet3,"4"=>$Fleet4,"5"=>$Fleet5,"6"=>$Fleet6);
// Convert an array of JSON-Strings to unified array of structured data..
foreach ( $Fleets as $key => $sJSONString ){
$FleetRaw[$key] = json_decode($sJSONString);
}
// Now return the whole lot as a json-string to the client
header("Content-type: application/json"); // My assumption of your model..
print json_encode($Fleets);
答案 0 :(得分:2)
据我所知,有两个问题:
问题A:数据库中的JSON损坏
Output 1:
{ “status” : 3, “game” : 0, “ships” : { "1" : { ... etc
这些字符“”在JSON中不合法......因此您将无法将数据库中的数据解析为JSON。你必须用合法的“字符替换它们.JSON来自哪里?
问题B:混合字符串&amp;结构强>
您正在混合使用JSON-as-a-string(来自数据库)和PHP中的数组数据结构(数据库中的行数组),您希望将其表示为JSON。
所以要解决这个问题应该是:
<?php
// Convert an array of JSON-Strings to unified array of structured data..
foreach ( $FleetRaw as $key => $sJSONString ){
$FleetRaw[$key] = json_decode($sJSONString);
}
// Now return the whole lot as a json-string to the client
header("Content-type: application/json"); // My assumption of your model..
print json_encode($FleetRaw);
?>
这应输出的是一个对象数组:
[{ "status" : 3, "game" : 0, "etc" : "..." },{ "status" : 99, "game" : 123, "etc" : "..." },{ "status" : 345, "game" : 456, "etc" : "..." },{ .. }]
请注意您的'nulls'和&amp; UTF8(输出3)
我认为你的空值是由PHP甚至无法将JSON字符串编码为字符串引起的,因为它们包含UTF8字符 - 这就是输出3 显示空值的原因。但是那些编码问题可能只是你数据库中的那些狡猾的“”。
如果你修复问题A,你可能会发现你也修复了输出3 。虽然这并不妨碍您必须解决问题B. 输出3 将成为您的JSON字符串数组(表示为恰好看起来像JSON的字符串)。问题B然后会把你排除在外。
顺便说一句:http://php.net/manual/en/function.json-last-error.php应该可以帮助您缩小源JSON的任何剩余问题,如果上述情况不符合的话。
希望这有帮助!学家
答案 1 :(得分:0)
在最后一行尝试json_decode()
,如下所示:
$Fleets["1"] = '{ "status" : 3, "game" : 0, "ships" : { "1" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 100 }, "3" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : -100 }, "2" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 0 }, "0" : { "level" : 0, "className" : "MotherShip", "posX" : 0, "health" : 100, "posY" : 0 } } }';
//Output 1
echo $Fleets["1"]."<br><br><br>";
//Output 2
echo json_encode(utf8_encode($Fleets["1"]))."<br><br><br>";
//Output 3
echo '<pre>';
print_r(json_decode($Fleets["1"]), true);
echo json_decode($Fleets["1"]);
您的输出1应为: -
{ "status" : 3, "game" : 0, "ships" : { "1" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 100 }, "3" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : -100 }, "2" : { "level" : 0, "className" : "LighteningShip", "posX" : 100, "health" : 50, "posY" : 0 }, "0" : { "level" : 0, "className" : "MotherShip", "posX" : 0, "health" : 100, "posY" : 0 } } }
它可能对你有帮助。
答案 2 :(得分:0)
我不确定,但我认为问题是您的车队数据已经是json格式。这是第一个输出回应你想要的方式。在第二个输出中,您只需将Fleets [“1”]中的json数据编码为utf8,然后再将其编码为json。第三个输出具有相同的问题,但这次你只是尝试将你的json数据重新编码为json。
试试这个:
$Fleet1 = json_decode($FleetRaw['Fleet1']);
$Fleet2 = json_decode($FleetRaw['Fleet2']);
$Fleet3 = json_decode($FleetRaw['Fleet3']);
$Fleet4 = json_decode($FleetRaw['Fleet4']);
$Fleet5 = json_decode($FleetRaw['Fleet5']);
$Fleet6 = json_decode($FleetRaw['Fleet6']);
你得到了对象。
$Fleets = array("1"=>$Fleet1,"2"=>$Fleet2,"3"=>$Fleet3,"4"=>$Fleet4,"5"=>$Fleet5,"6"=>$Fleet6);
你得到了一些对象
echo json_encode($Fleets);
你应该得到一个虚荣的json数据。