使用Post进行JSON数据解码和编码

时间:2016-02-09 18:18:06

标签: php arrays json

我希望获得一些信息,因为我是json的新手并且来回推送json数据。 这都是在php中完成的。

我目前以下列格式将json数据推送到我的php网络服务

stdClass Object
(
    [info] => stdClass Object
        (
            [applicantId] => 47713641-a5ca-4c6d-960d-b93bf0ca2742
            [lastName] => test
            [firstName] => test
            [middleName] => 
            [phone] => 111-111-1111
            [email] => test@gmail.com
            [birthDate] => 0001-01-01T00:00:00
            [sin] => 
            [workPermitNum] => 
            [street] => 123 street
            [suite] => 
            [city] => Toronto
            [province] => ON
            [postalCode] => 
            [nearestOffice] => 
            [pager] => 
            [cellPhone] => 
            [howDidYouHearAboutUs] => 
            [workedForOtherAgency] => 
            [otherAgencyName] => 
            [bondable] => 
            [positionDesired] => 
            [skillsCategory] => 0
            [classificationId] => 
            [skills] => 
            [documentLink] => 
            [referenceId] => 
            [website] => 
            [education] => 
            [country] => CA
            [workHistory] => stdClass Object
                (
                    [applicantId] => 47713641-a5ca-4c6d-960d-b93bf0ca2742
                    [education] => 
                    [companyName1] => TEST
                    [startDate1] => 2014-11-01T00:00:00
                    [endDate1] => 2015-08-01T00:00:00
                    [title1] => TEST
                    [reason1] => 
                    [super1] => 
                    [phone1] => 
                    [payRate1] => 
                    [desc1] => TEST description here
                    [companyName2] => TEST company 2
                    [startDate2] => 2013-04-01T00:00:00
                    [endDate2] => 2014-10-01T00:00:00
                    [title2] => test 2 title
                    [reason2] => 
                    [super2] => 
                    [phone2] => 
                    [payRate2] => 
                    [desc2] => test 2 description
                    [companyName3] => Company 3
                    [startDate3] => 2008-11-01T00:00:00
                    [endDate3] => 2013-03-01T00:00:00
                    [title3] => test title 3
                    [reason3] => 
                    [super3] => 
                    [phone3] => 
                    [payRate3] => 
                    [desc3] => test decription
                )

        )

)

使用以下内容解码json信息

$obj = json_decode($result);
        $info = $obj->{'info'};
        $applicantId = $info->{'applicantId'};
        $firstName = $info->{'firstName'};
        $lastName = $info->{'lastName'};
        $phone = $info->{'phone'};
        $mobile = $info->{'CellPhone'};
        $street = $info->{'street'};
        $city = $info->{'city'};
        $suite = $info->{'suite'};
        $postalCode = $info->{'postalCode'};
        $positionDesired = $info->{'positionDesired'};
        $website = $info->{'website'};
        $workHistory = $info->{'workHistory'};
        $companyName1 = $workHistory->{'companyName1'};
        $startDate1 = $workHistory->{'startDate1'};
        $endDate1 = $workHistory->{'endDate1'};
        $title1 = $workHistory->{'title1'};
        $reason1 = $workHistory->{'reason1'};
        $super1 = $workHistory->{'super1'};
        $phone1 = $workHistory->{'phone1'};
        $payRate1 = $workHistory->{'payRate1'};
        $desc1 = $workHistory->{'desc1'};
        $companyName2 = $workHistory->{'companyName2'};
        $startDate2 = $workHistory->{'startDate2'};
        $endDate2 = $workHistory->{'endDate2'};
        $title2 = $workHistory->{'title2'};
        $reason2 = $workHistory->{'reason2'};
        $super2 = $workHistory->{'super2'};
        $phone2 = $workHistory->{'phone2'};
        $payRate2 = $workHistory->{'payRate2'};
        $desc2 = $workHistory->{'desc2'};
        $companyName3 = $workHistory->{'companyName3'};
        $startDate3 = $workHistory->{'startDate3'};
        $endDate3 = $workHistory->{'endDate3'};
        $title3 = $workHistory->{'title3'};
        $reason3 = $workHistory->{'reason3'};
        $super3 = $workHistory->{'super3'};
        $phone3 = $workHistory->{'phone3'};
        $payRate3 = $workHistory->{'payRate3'};
        $desc3 = $workHistory->{'desc3'}; 

使用此数据,我将其填充到表单中 使用POST函数将此数据推送回Web服务我使用了以下数据数组

$data = array(
            "FirstName" => $_POST["firstName"],
            "LastName" => $_POST["lastName"],
            "ApplicantId" => $applicantId,
            "Phone"=>$_POST["phone"],
            "CellPhone"=>$_POST["mobile"],
            "PostalCode"=>$_POST["postalCode"],
            "Email"=>$_POST["email"],
            "City"=>$_POST["city"],
            "Street"=>$_POST["street"],
            "Website"=>$_POST["website"],
            "Country"=>$_POST["country"],
            "NearestOffice"=>$_POST["NearestOffice"],
            "Province"=>$_POST["province"],
            "HowDidYouHearAboutUs" =>$_POST["Ad"],
            "CompanyName1" =>$_POST["companyName1"],
            "StartDate1" =>$_POST["startDate1"],
            "EndDate1" =>$_POST["endDate1"],
            "Title1" =>$_POST["title1"],
            "Reason1" =>$_POST["reason1"],
            "Super1" =>$_POST["super1"],
            "Phone1" =>$_POST["phone1"],
            "PayRate1" =>$_POST["payRate1"],
            "Desc1" =>$_POST["desc1"],
            "CompanyName2" =>$_POST["companyName2"],
            "StartDate2" =>$_POST["startDate2"],
            "EndDate2" =>$_POST["endDate2"],
            "Title2" =>$_POST["title2"],
            "Reason2" =>$_POST["reason2"],
            "Super2" =>$_POST["super2"],
            "Phone2" =>$_POST["phone2"],
            "PayRate2" =>$_POST["payRate2"],
            "Desc2" =>$_POST["desc2"],
            "CompanyName3" =>$_POST["companyName3"],
            "StartDate3" =>$_POST["startDate3"],
            "EndDate3" =>$_POST["endDate3"],
            "Title3" =>$_POST["title3"],
            "Reason3" =>$_POST["reason3"],
            "Super3" =>$_POST["super3"],
            "Phone3" =>$_POST["phone3"],
            "PayRate3" =>$_POST["payRate3"],
            "Desc3" =>$_POST["desc3"]
        );

然后将其编码回Web服务调用 $ data_string = json_encode($ data); 但是,数据未保存在Web服务调用上,因为数据的格式不同。

如果有办法格式化我的帖子数据数组并以相同的方式编码,然后将其推回到网络服务?

这会有很大帮助!再次向Json致敬,对不起所有代码!

0 个答案:

没有答案