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 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"