Viewed   111 times

I'm trying to get the value from this following JSON array in a PHP variable.

This is a var_dump of the array:

array(3) {
  ["id"]=>
  string(2) "14"
  ["css"]=>
  string(400) ""
  ["json"]=>
  string(4086) "
            {
                "Canvas": [
                    {
                        "MainObjects": {
                            "After Participation": {
                                "afterParticipationHeader": "Thank you!"
                            },
                            "Invite Friends": {
                                "InviteHeadline": "",
                                "InviteMsg": "",
                                "InviteImg": ""
                            }
                        },
                        "QuizModule": {
                            "Questions": [],
                            "Submit_Fields": [
                                {
                                    "label": "Name",
                                    "name": "txtName",
                                    "value": true
                                }
                            ]
                        }
                    }
                ]
            }"
        }

I am able to get the values for ["json"] in PHP like:

$json = $data[0]['json'];

But how do I get the value from from the array inside "json", like "AfterParticipationHeader". And "Submit_Fields" ?

 Answers

5

First you have to decode your json data

$json = json_decode($data[0]['json']);

Then you can access your AfterParticipationHeader

$json->Canvas[0]->MainObjects->{"After Participation"}->afterParticipationHeader
Saturday, November 5, 2022
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
1

The reason you aren't able to json_decode($response) is because $response is an object, not a JSON string.

The Pin-PHP library will automatically decode all requests for you, as per https://github.com/noetix/pin-php/blob/master/src/Pin/Handler.php#L87

To see if the response has been successful, use the response object as an object:

if ($response->response->success) {
    echo "Success!";
} else {
    echo "Failed :(";
}
Sunday, October 9, 2022
 
kyogs
 
3

you are looking for attachments in wrong object. "attachmetnts" is property of item. instead of

JSONArray feedArray1 = response1.getJSONArray("attachments");

use

JSONArray feedArray1 = feedObj.getJSONArray("attachments");

in your case feedObj contains item object.

to get photo : Remove lines :

        String image = feedObj.isNull("photo") ? null : feedObj
                .getString("photo");
        item.setImge(image);

and change it to :

    for (int i1 = 0; i1 < feedArray1.length(); i1++) {
            JSONObject  attachment = (JSONObject) feedArray1.get(i1);
            JSONObject photo = (JSONObject) attachment.getJSONObject("photo");
            item.setImge(photo);
            item.setProfilePic(photo.getString("photo_75"));
            item.setStatus(photo.getString("text"));
         }
Thursday, September 1, 2022
 
shy
 
shy
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :