mysql数组到json转换

时间:2015-05-25 05:13:19

标签: php mysql json

我有一个问题,我试图让json响应看起来像这样:

{

"0":{
    "cid":"2",
    "user_name":"4",
    "user_title":null,
    "user_description":null,
    "user_tags":null,
    "user_imgid_logo":"05d419b4cc5c6d1d2fabf90a8dbd967a",
    "user_online":"2",
    "rank":"0",
    "multibitrate":"0",
    "user_name":"gqgeee",
    "user_logo_url":"http:\/\/static.example.tv\/logos\/05d419b4cc5c6d1d2fabf90a8dbd967a.jpg",
    "user_image":"1"
},
"1":{
    "cid":"1434",
    "user_name":"dokumenty",
    "user_title":"*Filmy dokumentalne* (^HQ^-^HD^)",
    "user_description":"short deskt",
    "user_tags":"asfasf",
    "user_imgid_logo":"51bbe341fe3e92f37a89609b80274be8",
    "user_online":"2",
    "rank":"1",
    "multibitrate":"1",
    "user_name":"asdd",
    "user_logo_url":"http:\/\/static.example.tv\/logos\/51bbe341fe3e92f37a89609b80274be8.jpg",
    "user_image":"1"
}}

我已经设法连接到mysql,并从数据库创建json,但我的json看起来不像上面那样:

[

{
    "1":{
        "cid":"1",
        "user_name":"sgsd",
        "user_title":"sgsd asgx",
        "user_description":"asgx na czasie",
        "user_tags":"asgx",
        "user_imgid_logo":"sgsd",
        "user_online":"2",
        "rank":"3",
        "multibitrate":"1",
        "user_name":"Kowalski",
        "user_logo_url":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/84\/asd.png",
        "user_image":"1"
    }
},
{
    "2":{
        "cid":"2",
        "user_name":"sgsd 2",
        "user_title":"sgsd asgx 2",
        "user_description":"asgx na czasie 2",
        "user_tags":"asgx",
        "user_imgid_logo":"sgsd",
        "user_online":"2",
        "rank":"3",
        "multibitrate":"1",
        "user_name":"Kowalski",
        "user_logo_url":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/8\/84\/sgsd.tv_logo_2014.png",
        "user_image":"1"
    }
}

]

^不需要括号,json仍然有点混乱。

这是我的php源代码:

<?php
$getoption = $_GET["option"];
if ($getoption === 'psd') {
//$dataList = substr($dataList, 1, -1);
//open connection to mysql db
$connection = mysqli_connect("localhost","datbase","duser","upass") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from public";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$num_rows = mysql_num_rows($result);
//create an array
//    $emparray[] = array();
//$emparray[] = array('1'=>$row);
echo '<pre>';
var_dump(mysqli_fetch_assoc($result));
echo '</pre>';
$start = 1;
while($row = mysqli_fetch_assoc($result))
{
$emparray[] = array($start=>array($row));
$start++;
}
//$dataList = substr(json_encode($emparray), 1, -1);
$dataList = json_encode($emparray);
echo $dataList;
//close the db connection
mysqli_close($connection);
}
if ($getoption === 'sk') {
echo 'ks os';
}
?>

任何人都愿意帮忙吗?感谢。

2 个答案:

答案 0 :(得分:0)

请试试这个......

 int rowIndex = 1;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow dataRow = sheet.CreateRow(rowIndex);

                for (int j = 0; j < cellCount; j++)
                {
                    cell = dataRow.CreateCell(j,CellType.String);
                    cell.SetCellValue(new HSSFRichTextString(dt.Rows[i][j].ToString()));
                }
                rowIndex++;
            }

答案 1 :(得分:0)

$start = 1;
while($row = mysqli_fetch_assoc($result))
{
$emparray[$start] = $row;
$start++;
}
$dataList = json_encode($emparray);
echo $dataList;

试试这个!!大多数情况下,while循环中的部分有点混乱。