1

Hello I am trying to use php to access my data base from a Swift app I am coding. So far reading the tables has been going great, except now I am trying to read a table that has multiple rows containing json. This has been throwing errors and I can not seem to get the final output to equal what I want, or anything that works with the swift code for that matter. The json originally just outputted as null. Upon researching how to fix that I tried utf8_encode() but that gave too many extra characters and the Swift code in the app couldn't make sense of it. When outputting just one of the rows it comes out fine, Its when I try putting them in one associative array to out put as json is when they come up as null.

PHP Code:

$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);

?>

Outputs:

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}

Output 1 is exactly the format I want (the one Swift understands), except it is only one of the six rows (Also app rejects this form because it is not json_encode before echoing). Output 2 is an example of one of the six rows that when used utf8_encode() before saved to the array gives to many extra characters, however it does output as not null when put into an array of the six. Output 3 is what I want to eventually output, just without the null.

The ideal situation would be to combine outputs 1 and 3 so that I can output an array of six with them looking like Output 1. Also the app has only worked when I json_encode what I echo. If there is anyone possible to accomplish this please let me know!!

Thanks!!

closest attempt, working but double the data?

$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);
2
  • Please add some code formatting to your question Commented Dec 9, 2015 at 5:04
  • never mind just noticed their different style quotes than the rest Commented Dec 9, 2015 at 6:21

3 Answers 3

2

There are two problems, as far as I can see:

Issue A: Broken JSON in database

Output 1:
{ “status” : 3, “game” : 0, “ships” : { "1" : { ... etc

Those characters “” are not legal in JSON... so you won't be able to parse the data you have within your database as JSON. You will have to replace them with legitimate " characters. Where did the JSON come from?

Issue B: Mixed string & structure

You're mixing JSON-as-a-string (coming from the database) and an array data-structure in PHP (the array of rows from the database) that you wish to represent as JSON.

So to fix that should be doing something like:

<?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);

?>

What this should output is an array of objects:

[{ "status" : 3, "game" : 0, "etc" : "..." },{ "status" : 99, "game" : 123, "etc" : "..." },{ "status" : 345, "game" : 456, "etc" : "..." },{ .. }]

Note on your 'nulls' & UTF8 (Output 3)

I would imagine your nulls are caused by PHP failing to even encode the JSON-strings as strings because they contain UTF8 characters -- which is why the Output 3 shows nulls. But those encoding issues may just be those dodgy “” you have in your database.

If you fix issue A you may find you fix Output 3 too. Though this doesn't preclude you from having to address issue B. Output 3 would become an array of your JSON-strings (represented as strings that just happen to look like JSON). Issue B will then sort you out.

Incidentally: http://php.net/manual/en/function.json-last-error.php should help you narrow down any remaining issues with your source JSON if the above doesn't.

Hope this helps! J.

Sign up to request clarification or add additional context in comments.

3 Comments

can you give an example of what the correct json should look like (Issue A)
Ok that worked, sorta. I can read the data but for some reason I'm getting double the data. I pasted the code I used under my original question. I couldn't loop through the $FleetRaw as you proposed because it has more columns than just the six fleets I specifically got
All sorted? No worries :)
0

I'm not sure but I think the problem is that your fleet data is already in json format. That is way the first output echoes what you want. In the second output you just encode json data from Fleets["1"] into utf8 and then encode it into json again). The third output with the same problem but this time you just trying to reencode your json data into json again.

Try this one:

$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']);

You get objects.

$Fleets = array("1"=>$Fleet1,"2"=>$Fleet2,"3"=>$Fleet3,"4"=>$Fleet4,"5"=>$Fleet5,"6"=>$Fleet6);

You get array of objects

echo json_encode($Fleets);

You should get a vaild json data.

6 Comments

That could definitely be it! Except when I tried I get the error: Warning: json_decode() expects parameter 1 to be string, array given in /GetFleet.php on line 33
Are you expecting just one row from mysql_fetch_assoc($result);? If not you need to use while loop.
Yes just one row, I have LIMIT 1
Ok. Well, then you need to make sure you have valid string types in your $FleetRaw['Fleet1'], $FleetRaw['Fleet2']... variables. Otherwise it's hard to analyse your code. Try to echo each of them and see if they are vaild strings.
Isn't output 1 an example of what your asking my to echo? or are you asking for something else?
|
0

Try to json_decode() in last line as follows:

$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"]);

Your output 1 should be :-

{ "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 } } }

It may help you.

8 Comments

received following error: Warning: json_decode() expects parameter 1 to be string, array given in /GetFleet.php on line 36
theres no output 3, not an error either, just nothing
can you please try one more time?
Im more than happy to keep trying whatever ideas you have. But there still is not output 3
if you try to validate your output1 json with jsonlint its not pure json.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.