为什么我的JSON字符串为null? [英] Why my JSON string is null?

查看:426
本文介绍了为什么我的JSON字符串为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用PHP编写了代码,以查看JSON字符串输出.但是我得到的是空值.

I have written a code in PHP , to see the JSON string output. But i am getting null value.

<?php

 $l=array();

 $l[] = array('a'=>'@cÐaÐjÔÐ J kf _ÞÒi^ ','b'=>']éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi');

 echo $j = json_encode($l);

?>

输出-:

  [{"a":null,"b":null}] 

为什么JSON输出为空.我期望一定会有字符编码问题.

Why the JSON output is coming in null. I am expecting that there must be character encoding problem.

我想要以下格式的输出.

i want the output in following format.

[{"a":"@cÐaÐjÔÐ J kf _ÞÒi^","b":"]éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi"}] 

请帮帮我.请给我建议一些解决方法

Please help me out. Please suggest me some solution

先谢谢了!

推荐答案

您可能需要使用 utf8_encode()在推送到数组之前然后是json_encode之前的字符串,因为 json_encode()仅适用于utf8编码的数据

You may need to use utf8_encode() the string before pushing to array and then json_encode since json_encode() works only only with utf8 encoded data

$l=array();

$l[] = array('a'=>utf8_encode('@cÐaÐjÔÐ J kf _ÞÒi^ '),
'b'=>utf8_encode(']éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi'));

echo json_encode($l);

好吧,看来您的问题并非如此简单,您需要使用

Ok looks like your issue is not so simple,and you need to use

htmlentities( (string) $value, ENT_QUOTES, 'utf-8', FALSE); 

处理情况

$array = array("a"=>htmlentities( (string) "@cÐaÐjÔÐ J kf _ÞÒi^ ", ENT_QUOTES, 'utf-8', FALSE),
               "b"=>htmlentities( (string) "]éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi", ENT_QUOTES, 'utf-8', FALSE)

             );

$json = json_encode($array);
echo ($json);

在此处检查 http://phpfiddle.org/main/code/mh8-7ua

您需要在数组元素中添加以上内容.

You need to add as above in your array elements.

这篇关于为什么我的JSON字符串为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆