Re: help parsing json with php
| From: | Aziz Saleh | Date: | Sun, 27 Jan 2019 00:44:59 +0000 |
| Subject: | Re: help parsing json with php | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-326687@lists.php.net to get a copy of this message | ||
The issue here is that Name property has a space before it. So " Name" is
what you are looking for and not "Name". Something like this would work:
print_r($data->data->lead2->{" Name"});
On Sat, Jan 26, 2019 at 7:41 PM dealtek@gmail.com <dealtek@gmail.com> wrote:
> Thanks so much Aziz,
>
> That is very helpful.
>
> I see you had:
>
> <?php
> $data = '
>
> {"riddleId":179849,"data":{"riddle":{"id":179849,"title":"test1","type":"quiz"},"lead2":{"
>
> Name":{"value":"ddddd","type":"text"},"Email":{"value":"b@b.com","type":"email"}},"answers":[{"question":"<p>q1<\/p>","answer":"a1","correct":true,"quest
>
> ionId":1,"answerId":1}],"result":1,"resultData":{"scoreNumber":1,"resultIndex":1,"scorePer
> centage":100,"scoreText":"Your score:
>
> 1\/1","resultId":2},"embed":{"parentLocation":"https:\/\/www.riddle.com\/showcase\/179849
> \/quiz"}}}';
>
> $data = json_decode($data);
>
> print_r($data->data->riddle);
>
> ---
>
> Sorry to say I still need one bit more help:
>
> I want to display the --- lead2 --- name:
>
>
> I tried:
> print_r($data->data->lead2);
> and this gives...
>
> stdClass Object ( [ Name] => stdClass Object ( [value] => ddddd [type] =>
> text ) [Email] => stdClass Object ( [value] => b@b.com [type] => email )
> )
>
> but when I try this:
>
> print_r($data->data->lead2->Name->value);
> or
> rint_r($data->data->lead2->Name);
>
> it comes back empty.
>
>
> Q: How do I display ...lead2... name?
>
> THANKS MUCH
>
> Dave
>
>
>
> On Jan 26, 2019, at 2:31 PM, Aziz Saleh <azizsaleh@gmail.com> wrote:
>
> Hi Dave,
>
> That response looks like a valid JSON, json_decode should suffice to get
> any data:
>
>
>
> http://sandbox.onlinephpfunctions.com/code/1cd4aeaf91145831a1bab022ed6fbc98d43adc1c
>
> Aziz
>
> On Sat, Jan 26, 2019 at 5:09 PM dealtek@gmail.com <dealtek@gmail.com>
> wrote:
>
>> Hi all,
>>
>> I could use a bit of help parsing json (from riddle.com) with php.
>>
>> the php result page shows:
>>
>>
>> <?php
>> $riddledata = date('Y-m-d H:i:s') . ' ' . print_r($_REQUEST, true) .
>> "\n";
>>
>> file_put_contents('webhook-logs.log', $riddledata, FILE_APPEND);
>>
>> ?>
>>
>> and the $_REQUEST data looks like:
>>
>>
>> {"riddleId":179849,"data":{"riddle":{"id":179849,"title":"test1","type":"quiz"},"lead2":{"
>>
>> Name":{"value":"ddddd","type":"text"},"Email":{"value":"b@b.com","type":"email"}},"answers":[{"question":"<p>q1<\/p>","answer":"a1","correct":true,"quest
>>
>> ionId":1,"answerId":1}],"result":1,"resultData":{"scoreNumber":1,"resultIndex":1,"scorePer
>> centage":100,"scoreText":"Your score:
>>
>> 1\/1","resultId":2},"embed":{"parentLocation":"https:\/\/www.riddle.com\/showcase\/179849x
>> ^”.
è^NS5-
>> \/quiz"}}}
>>
>>
>>
>> or maybe this after print_r($_REQUEST, true) ???
>>
>>
>>
>> (
>>
>> [data] =>
>>
>> {"riddleId":179849,"data":{"riddle":{"id":179849,"title":"test1","type":"quiz"},"lead2":{
>>
>> "Name":{"value":"aaaaaa","type":"text"},"Email":{"value":"b@b.com","type":"email"}},"answers":[{"question":"<p>q1<\/p>","answer":"a1","correct":true,"quest
>>
>> ionId":1,"answerId":1}],"result":1,"resultData":{"scoreNumber":1,"resultIndex":1,"scorePer
>> centage":100,"scoreText":"Your score:
>>
>> 1\/1","resultId":2},"embed":{"parentLocation":"https:\/\/www.riddle.com\/showcase\/179849
>> \/quiz"}}}
>>
>> )
>>
>>
>> --- I tried parsing like:
>>
>> $form_data = json_decode($_REQUEST);
>>
>> $riddleId = $form_data->riddleId[0];
>>
>> --- also
>>
>> $name = ' lead2 name
>> '.$_REQUEST['data']['lead2']['Name']['value'];
>>
>> but nothing is working yet.
>>
>>
>> Q: What's the best way to parse this out and get the varied fields
>> showing with php?
>>
>> --
>> Thanks,
>> Dave - DealTek
>> dealtek@gmail.com
>> [db-19]
>>
>>
>
> --
> Thanks,
> Dave - DealTek
> dealtek@gmail.com
> [db-19]
>
>