duancenxiao0482 2015-11-27 03:48
浏览 9
已采纳

使用PHP格式错误的JSON

I am a .NET guy with fairly long experience but recently pulled into a PHP project so need some help. I have this PHP code which prepares a final song object and then JSONifies it with json_encode() but I strangly I am seeing some extra value null added at the end of JSON string and I dont seem to understand why is that.

Here is my PHP code

foreach($result as $item) {
    ++$index;
    $hymn->id=$item['id'];
    $hymn->refrain=$item['refrain'];

    if ($index == 1) { $hymn->stanza1=$item['stanzaText']; }
    if ($index == 2) { $hymn->stanza2=$item['stanzaText']; }
    if ($index == 3) { $hymn->stanza3=$item['stanzaText']; }
    if ($index == 4) { $hymn->stanza4=$item['stanzaText']; }
    if ($index == 5) { $hymn->stanza5=$item['stanzaText']; }
    if ($index == 6) { $hymn->stanza6=$item['stanzaText']; }
    if ($index == 7) { $hymn->stanza7=$item['stanzaText']; }
    if ($index == 8) { $hymn->stanza8=$item['stanzaText']; }
    if ($index == 9) { $hymn->stanza9=$item['stanzaText']; }
    if ($index == 10) { $hymn->stanza10=$item['stanzaText']; }
}

json_encode($hymn);

Above code produces a JSON with extra null after the ending curly braces as shown below.

{
    "id":"1",
    "refrain" : "Jesus, Jesus, how I trust Him!
How I\u2019ve proved   Him o\u2019er and o\u2019er;
Jesus, Jesus, precious Jesus!
Oh, for grace to trust Him more!",
    "stanza1" : "Tis so sweet to trust in Jesus,
Just to take Him at His Word;
Just to rest upon His promise,
And to know, \u201cThus saith the Lord!\u201d",
    "stanza2" : "Oh, how sweet to trust in Jesus,
Just to trust His cleansing blood;
And in simple faith to plunge me
\u2019Neath the healing, cleansing flood!",
    "stanza3" : "Yes, \u2019tis sweet to trust in Jesus,
Just from sin and self to cease;
Just from Jesus simply taking
Life and rest, and joy and peace.",
    "stanza4" : "I\u2019m so glad I learned to trust Thee,
Precious Jesus, Savior, Friend;
And I know that Thou art with me,
Wilt be with me to the end"
 }null

Kindly help me to understand what wrong am I doing here?

  • 写回答

2条回答 默认 最新

  • doubao7287 2015-11-27 13:41
    关注

    All friends, I apologize as I should have made this an answer before but problem is not with JSON. It is with fetchAll(). Thanks for analyzing and helping me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器