无法在php中解码JSON字符串 [英] Can't decode JSON string in php

查看:140
本文介绍了无法在php中解码JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON字符串,我尝试使用php json_decode进行解码,但使用$ postarray 始终为NULL,无法找出原因为何?

I have the following JSON string, i try to decode with php json_decode but $postarray is always NULL, can't work out why this is?

在Debian 5.0 Linux上运行 php客户端API版本=> 5.0.51a Json版本1.2.1

Running on Debian 5.0 Linux php Client API version => 5.0.51a Json version 1.2.1

 $json = '{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}';

 $postarray = json_decode($json);
 print_r($postarray);

谢谢

推荐答案

在字符串中使用双引号(\")进行转义的原因是,如果该字符串用双引号引起来.

The reason to escape double quotes (\") in a string, is if the string is double quoted.

因为要转义双引号,所以应该对字符串加双引号(而不是单引号),如下所示:

Since you are escaping the double quotes, you should double (not single) quote your string, like this:

<?php
 $json = "{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}";

 $postarray = json_decode($json);
 print_r($postarray);
?>

> 实时示例

如果您想单引号,请 不要用双引号 > ,或使用 stripslashes() 就像安德烈(Andrei)建议的那样.

If you do want to single quote your string, then don't escape the double quotes, or use stripslashes() like Andrei suggested.

您可以阅读有关> 在PHP中指定字符串的四种方法,以及它们之间的区别,在这里 .

You can read about the four ways to specify a string in PHP, and the differences among them, here.

这篇关于无法在php中解码JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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