简体   繁体   English

使用PHP的mysql数据库的JSON响应显示不正确的格式

[英]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. 这是我的php文件中的getforum函数。 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. 这使得论坛值具有以uid值表示的数组,您可以为其设置每个条目的数据。

It's not possible. 这是不可能的。 Javascript arrays must have sequential key numering, as the [...] syntax doesn't allow you to specify keys. javascript数组必须具有顺序的键编号,因为[...]语法不允许您指定键。 Since you're using ID numbers, which almost certainly won't be sequential or without gaps, the "array" must be encoded as an object. 由于您使用的ID号几乎肯定不会是连续的或没有间隙,因此必须将“数组”编码为一个对象。

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: 至于原因:由于array [] -shortcut表示法无法指定键,因此json编码引擎将被迫直接指定EVERY SINGLE缺少的键,因此请考虑简并的情况:

$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". JSON绝对是大规模的,具有999,999,998份x:null副本,只是为了“填补空白”。

Thats because you are not adding as array elements , but you are adding as assoc-array element with keys , 那是因为您不是要添加为数组元素,而是要使用键添加assoc-array元素,

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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