PHP中的高级Json对象处理

时间:2017-07-07 17:07:24

标签: php json object decode encode

我正在尝试将一些Json数据发送到我的服务器端PHP代码,但是我收到了一条错误消息:

  

注意:尝试获取非对象的属性   第xy行的C:\ xampp \ htdocs \ registration.php。

这是我的Json对象。我想要处理的事情:

   registrationInputData:{"page1":{"regfnev":"John","reglnev":"Kerry","regemail":"john.kerry@gmail.com","regpassword":"Qwerty01"},"page2":{"regtelepules":"Budapest","regirsz":"1123","regutca":"","reghazszam":"","regemelet":"","regajto":"","regtelszam":""},"page3":{"regprofilimage":"dogProfileImage","regfeltetel":true}}

这是我的PHP:

<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "getpet");
mysqli_set_charset($conn, "utf8");
$result = false;

if(isset($_POST['registrationInputData'])){
$registrationInputData = json_encode($_POST['registrationInputData']);

///page1
$fname = $registrationInputData->page1->regfnev;
$lname = $registrationInputData->page1->reglnev;
$email = $registrationInputData->page1->regemail;
$password = md5($registrationInputData->page1->regpassword);

$emailquery = "SELECT email FROM users WHERE email = '".$email."'";
$emailsql = mysqli_query($conn, $emailquery);
if(mysqli_num_rows($emailsql) == "0"){

    ///page2
    $settlement = $registrationInputData->page2->regtelepules;
    $postcode = $registrationInputData->page2->regirsz;
    $street = $registrationInputData->page2->regutca;
    $streetnumber = $registrationInputData->page2->regutca;
    $floor = $registrationInputData->page2->regemelet;
    $door = $registrationInputData->page2->regajto;
    $phone = $registrationInputData->page2->regtelszam;

    ///page3
    $profilimage = $registrationInputData->page3->regprofilimage;
    $conditionaccepted = $registrationInputData->page3->regfeltetel;

    $registrationquery =
    "INSERT INTO users (fname, lname, email, password, settlement, postcode, street, streetnumber, floor, door, phone, profilimage, conditionaccepted)
     VALUES ('".$fname."', '".$lname."', '".$email."', '".$password."', '".$settlement."', '".$postcode."', '".$street."', '".$streetnumber."', '".$floor."', '".$door."', '".$phone."', '".$profilimage."', '".$conditionaccepted."' )";
     $result = true;
}
}
echo $result;

?>

感谢您的回答!

1 个答案:

答案 0 :(得分:2)

如果是json字符串,你需要使用json_decode函数。然后,您可以访问对象元素。