【问题标题】:Issue with json encode of database string value数据库字符串值的 json 编码问题
【发布时间】:2015-09-01 10:40:35
【问题描述】:

我在 php 数组中有如下数据库查询结果:

Array
(
    [status] => 1
    [message] => Successfully Login
    [request] => Array
        (
            [firstName] => Jugni
            [lastName] => Patel
            [popup_message] => To login to this portal you have to agree out terms and condition
• L80 – R169 pm x 24 on SMARTCHAT 1GB
• G3 Beat – R259 pm x 24 on SMARTCHAT 1GB
• G3 – R399 pm x 24 on SMARTCHAT 1GB
Top 5 MTN Sales consultants for period 2-8 March 2015, on G3 & G3 Beat sales will WIN R500 each! You have until Monday 9 March 9AM to log your sales for this competition!
        )

)

当我对上述数组进行 json_encode 时,它​​会在描述中给出空值。

{"status":"1","message":"Successfully Login","request":{"firstName":"Jugni","lastName":"Patel","popup_message":null}}

更新:

现在我可以使用 utf8_encode 方法获取描述的值。 但是当我再次使用 json_decode 解码该字符串时,它会给出以下输出:

它给出的解码值如下:

stdClass Object
(
    [status] => 1
    [message] => Successfully Login
    [request] => stdClass Object
        (
            [firstName] => Jugni
            [lastName] => Patel
            [popup_message] => To login to this portal you have to agree out terms and condition
• L80 – R169 pm x 24 on SMARTCHAT 1GB
• G3 Beat – R259 pm x 24 on SMARTCHAT 1GB
• G3 – R399 pm x 24 on SMARTCHAT 1GB
Top 5 MTN Sales consultants for period 2-8 March 2015, on G3 & G3 Beat sales will WIN R500 each! You have until Monday 9 March 9AM to log your sales for this competition!
        )

)

有人知道如何在 json_encode 之后获得相同的描述字段值吗?

【问题讨论】:

  • 如何创建php数组?你能贴出你在其中添加“popup_message”的代码吗?
  • $result=array('status'=> '1',"message"=>"成功登录","re​​quest"=>array("firstName"=>$row['Name' ],"lastName"=>$row['Surname'],"popup_message"=>($row['Description']))); } print_r(json_encode($result));
  • 我看到你编辑的答案,你得到这个数据是因为你打印结果的页面不是用 UTF-8 编码的。将<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 放在页面顶部的 php 标记之外。

标签: php json decode encode


【解决方案1】:

请确保您的数组中的字符串是utf8-encoded。 json_encode 需要 utf8 编码的字符串,并且会在失败时返回 false。

【讨论】:

  • 感谢您的回复。它在使用 utf8_encode 方法解码该编码字符串时工作但面临问题。
  • 你必须使用utf8_decode来解码utf8编码的字符串。
猜你喜欢
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 2014-01-08
相关资源
最近更新 更多