dongmubei7950 2013-04-30 05:01
浏览 51
已采纳

我有来自mysql的数据并且错误地转换为JSON

I have the following table:

-> +-----+---------+-----+-------+--------------+
-> | id1 | fname   | id2 | fname | relationship |
-> +-----+---------+-----+-------+--------------+
-> |   4 | Albaraa |   5 | Sadi  | Father       |
-> +-----+---------+-----+-------+--------------+

I assign the following to the variable $relations:

$relations = $stmt->fetchAll(PDO::FETCH_ASSOC);

Finally I print out the JSON encode of this variable using this:

echo json_encode($relations);

...and I get the following output:

[{"id1":"4","fname":"Sadi","id2":"5","relationship":"Father"}]

So here is a few questions about the output that I get:

1) You can see that my output does not seem right as it should be something like this:

[{"id1":"4","fname":"Albaraa","id2":"5","fname":"Sadi","relationship":"Father"}]

Why is it not printing correctly, does it have something to do with the fact that the table, has two columns with the same name "fname"? If there is any input on how to fix this that would be amazing!

2) Another thing I was curious to know is that I actually want to change the JSON to where it becomes something like this:

[{"id":"4","name":"Albaraa","id":"5","name":"Sadi","relationship":"Father"}]

Is there an easy way to change "id1" and "id2" to become "id" and both "fname" to become just "name"?

Again any input on this will be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • douzhanhui5662 2013-04-30 05:03
    关注

    1) fname was overwritten. Try use different aliases in SQL query for matching fields.

    2) You should not pass exact name matching properties. They might be overwitten by JSON-decoder. Better to pass them as a different JSON-parts.

    [{"id":"4","name":"Albaraa","id":"5","name":"Sadi","relationship":"Father"}]
    

    Should be like:

    {
        "person1":{"id":4,"name":"Albaraa"},
        "person2":{"id":5,"name":"Sadi"},
        "relationship":"Father"
    }
    

    UPDv1:

    For result:

    -> +-----+---------+-----+--------+--------------+
    -> | id1 | fname1  | id2 | fname2 | relationship |
    -> +-----+---------+-----+--------+--------------+
    -> |   4 | Albaraa |   5 | Sadi   | Father       |
    -> +-----+---------+-----+--------+--------------+
    

    Try to convert like this:

    <?php
    // $row is sql fetched row.
    
    $person1  = array('id' => $row['id1'], 'name' => $row['fname1']);
    $person2  = array('id' => $row['id2'], 'name' => $row['fname2']);
    $response = array(
         'person1' => $person1,
         'person2' => $person2,
         'relationship' => $row['relationship']
    );
    
    echo json_encode($response);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行