简体   繁体   中英

JSON response from mysql database using PHP shows incorrect format

function getForum($class_divisionId) {
    $qry = "SELECT fm.uid,fm.class_divisionId,fm.username,fm.description,fm.date_time,si.name FROM forum as fm INNER JOIN studentinfo as si ON fm.username = si.username WHERE fm.class_divisionId='".mysql_real_escape_string($class_divisionId)."'";

    $exe = mysql_query($qry);

    if (mysql_num_rows($exe)> 0) {
        $response['error'] = 'false';
        while($result = mysql_fetch_array($exe)){
            $uid = $result['uid'];
            $response['forum'][$uid]['uid'] = $result['uid'];
            $response['forum'][$uid]['class_divisionId'] = $result['class_divisionId'];
            $response['forum'][$uid]['username'] = $result['username'];
            $response['forum'][$uid]['description'] = $result['description'];
            $response['forum'][$uid]['date_time'] = $result['date_time'];
            $response['forum'][$uid]['name'] = $result['name'];  
        }
        return $response;
    } else {
        return false;
    }
}

This is the getforum function in my php file. The response I am getting is:

{
    "error": "false",
    "forum": {
        "4": {
            "uid": "4",
            "class_divisionId": "ABC",
            "username": "XYZ_1",
            "description": "Forum description number 2",
            "date_time": "2015-08-15 10:17:18",
            "name": "XYZ"
        }
   }
}

I would like the response to be in the following format:

{
    "error": "false",
    "forum": [
        {
            "uid": "4",
            "class_divisionId": "ABC",
            "username": "XYZ_1",
            "description": "Forum description number 2",
            "date_time": "2015-08-15 10:17:18",
            "name": "XYZ"
        }
    ]
}

Can someone please advise what I am doing wrong?

use

$response['forum'][$uid]->'uid' = $result['uid'];
$response['forum'][$uid]->'class_divisionId' = $result['class_divisionId'];

This makes the forum value have an array denoted by uid values, for which you can set each entry's data.

It's not possible. Javascript arrays must have sequential key numering, as the [...] syntax doesn't allow you to specify keys. Since you're using ID numbers, which almost certainly won't be sequential or without gaps, the "array" must be encoded as an object.

eg

$foo = array(0 => 'a', 3 => 'b'); // missing keys 1 & 2
echo json_encode($foo);           // {"0":"a","3":"b"}
$bar = array('a', 'b');            // implicit keys 0,1
echo json_encode($bar);           // ["a","b"]
$baz = array(0 => 'a', 1 => 'b')  // explicit keys 0,1
echo json_encode($baz);           // ["a","b"]

As to why: since array [] -shortcut notation cannot specify keys, the json-encoding engine would be forced to specify EVERY SINGLE missing key directly, so consider a degenerate case:

$foo = array(0 => 'a', 999999999 => 'b');

The JSON would be absolutely MASSIVE, with 999,999,998 copies of x:null , just to "fill in the blanks".

Thats because you are not adding as array elements , but you are adding as assoc-array element with keys ,

this will have the output you posted

if (mysql_num_rows($exe)> 0) {
        $response['error'] = 'false';
        $response['forum'] = Array();
        while($result = mysql_fetch_array($exe)){
            $response['forum'][] = Array(
                                        'uid' => $result['uid'],
                                        'class_divisionId' => $result['class_divisionId'],
                                        'username' => $result['username'],
                                        'description' => $result['description'],
                                        'date_time' => $result['date_time'],
                                        'name' => $result['name']
                                        );
        }
        return $response;
    } else {
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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