PHP json_decode为验证json字符串抛出错误

时间:2013-07-18 08:13:12

标签: php json

我有以下错误

Notice:  Trying to get property of non-object in action.php

发布json已经验证(通过jsonLint.com验证)。

这是我的json字符串:

[
{
    "eTGid": "1",
    "eTid": "34",
    "evrakGelisTarihi": "12/12/2013",
    "evrakKonu": "Sertifika denemesi",
    "evrakKurumID": "1047",
    "evrakCikisTarihi": "13/12/2013",
    "evrakCikisSayisi": "313213213213",
    "aciklamaBolumu": "açıklayıcı notlar",
    "gelenEvrakTarihi": "30/12/2013",
    "gelenEvrakSayisi": "3132321",
    "gelenEvrakEtakipNo": "987654",
    "bagliIlaclar": "[\"0\",\"[{\\\"ilacID\\\":\\\"744\\\",\\\"ilacPN\\\":\\\"asdasd2132\\\",\\\"ilacSKT\\\":\\\"12/12/2013\\\"}]\"]",
    "bagliFirmalar": "[\"0\",\"[{\\\"firmaID\\\":\\\"1047\\\"}]\"]",
    "": "[\"0\",\"[{\\\"bankaID\\\":\\\"5\\\",\\\"makbuzNO\\\":\\\"asdasda\\\",\\\"makbuzTARIHI\\\":\\\"12/12/2013\\\",\\\"ihracaatYapilacakUlkeID\\\":\\\"2\\\",\\\"ilacIhracADI\\\":\\\"ABFADER\\\",\\\"makbuzTUTAR\\\":\\\"202,06\\\",\\\"makbuzTipDetayDEGERİ\\\":\\\"10\\\"}]\",\"[{\\\"bankaID\\\":\\\"5\\\",\\\"makbuzNO\\\":\\\"ASDAWW\\\",\\\"makbuzTARIHI\\\":\\\"12/12/2013\\\",\\\"ihracaatYapilacakUlkeID\\\":\\\"191\\\",\\\"ilacIhracADI\\\":\\\"ABFADEX\\\",\\\"makbuzTUTAR\\\":\\\"202,06\\\",\\\"makbuzTipDetayDEGERİ\\\":\\\"9\\\"}]\"]",
    "bagliMakbuzlar": "[\"0\",\"987654»12/12/2013»3213213\"]",
    "kurumIcimi": "hayir"
}
]

我的php代码是:

$gelenJsonVerisi = $_POST['yeniEvrak'];
echo($gelenJsonVerisi);
$yeniEvrakObj = json_decode($gelenJsonVerisi);

exit($yeniEvrakObj->{'eTGid'});

我哪里出错了?

建议后:

  1. 我的Json字符串已作为数组(括号内)到达服务器端(php)。
  2. Array只有一个元素(成员),它是我们的json字符串(对象)
  3. 处理数组的第一个元素并将其分配给php对象并处理它。

    $gelenJsonVerisi = $_POST['yeniEvrak'];
    $yeniEvrakObjArray = json_decode($gelenJsonVerisi,TRUE);
    $yeniEvrakObj = $yeniEvrakObjArray[0];
    exit($yeniEvrakObj['eTGid']); // one of sample value
    

    谢谢

4 个答案:

答案 0 :(得分:2)

$gelenJsonVerisi = $_POST['yeniEvrak'];
echo($gelenJsonVerisi);
$yeniEvrakObj = json_decode($gelenJsonVerisi);
exit($yeniEvrakObj[0]->eTGid);

答案 1 :(得分:2)

这不是json_decode抛出错误,而是当您尝试访问生成的数组时。是的,没错,数组。您的JSON值是:

[ { ... } ]
^  array  ^

所以你需要访问结果,如:

$yeniEvrakObj[0]->eTGid

答案 2 :(得分:2)

JSON字符串显示一个包含单个对象的数组。像这样访问数据:

$yeniEvrakObj = json_decode($gelenJsonVerisi);
echo $yeniEvrakObj[0]->eTGid;

如果您确定该数组中只有1个对象,那么可以尝试:

$yeniEvrakObj = json_decode(
                    substr($gelenJsonVerisi,1,-1)
);

切断前导和终止括号。这意味着没有尾随空格的前导,所以首先是trim字符串。

check codepad。如您所见,json_decode调用将数据作为包含对象的数组返回:

Array
(
    [0] => stdClass Object
        (
            [eTGid] => 1
            [eTid] => 34
            [evrakGelisTarihi] => 12/12/2013
            [evrakKonu] => Sertifika denemesi
            [evrakKurumID] => 1047
            [evrakCikisTarihi] => 13/12/2013
            [evrakCikisSayisi] => 313213213213
        )

)

答案 3 :(得分:-1)

问题在于您尝试访问已解码对象的方式,因为它位于数组中。 你的代码应该是:

$gelenJsonVerisi = $_POST['yeniEvrak'];
echo($gelenJsonVerisi);
$yeniEvrakObj = json_decode($gelenJsonVerisi);

exit($yeniEvrakObj[0]->eTGid);

编辑:感谢您对此回答的评论让我觉得我错了