创建JSON显示错误找到无效字符

时间:2016-02-25 18:43:49

标签: json

您正在尝试创建一个JSON对象,但它显示错误无效字符,发现这是我的JSON。

/**
 * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="articles")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
  protected $user;

   /**
     * Set user
     *
     * @param \UserBundle\Entity\User $user
     * @return Article
     */
    public function setUser(\UserBundle\Entity\User $user = null)
    {
        //This line is the problem
        $this->user = $this->container->get('security.context')->getToken()->getUser();

        return $this;
    }

请帮助解决此问题,并解释在使JSON有用时无效字符的情况。

2 个答案:

答案 0 :(得分:2)

JSON字符串中的换行符必须转义为\n。 JSON可以采用任何Unicode字符 - 除了 - " - 或 - 控制字符。您的JSON应如下所示:

{
    "title": "Biology",
    "content": "Egg period: 4 -6 days \n Eggs laid in cracks and crevices of the loose bark on the trunk \n Eggs: ovoid or elliptical and dirty white in colour \n Adult :Reddish brown in colour",
    "isSubtitle": "N"
}

尝试使用jsonlint.com验证未来。

答案 1 :(得分:2)

这是一种无效的JSON格式,内容不应包含换行符,因此您应该将其指定为\\n。 所以有效的格式是:

{
    "title": "Biology",
    "content": "Egg period: 4 -6 days \\n Eggs laid in cracks and crevices of the loose bark on the trunk \\n Eggs: ovoid or elliptical and dirty white in colour \\n Adult: Reddish brown in colour ",
    "isSubtitle": "N"
}