Domanda

Is there any way to add function to JSON object with php?

i have an array in php like this :

$aoData = array(
array('name' => 0, 'value' => "id"),
array('name' => 1, 'value' => "title"),
array('name' => 2, 'value' => "cat"),
array('name' => 3, 'value' => "img"),
array('name' => 4, 'value' => "des"));

and i want add a function like this:

array('name' => 5, 'value' => function(){return "hi"})

and use this function in my jquery.

is there any way for this?

Update

in data table i need to set aoColumnDefs in php.

    "aoColumnDefs": 
    [{
        "aTargets": [ img ],
        "mData": "thumb_img",
        "mRender": function ( data, type, full ) 
            return '<img src="../up/thumb/'+data+'">';
            },
        {etc}
        ]

so i need set function as a Json function or another way ...

È stato utile?

Soluzione 3

So yeah... JS Object is not the same as JSON.

Some libs or frameworks will want to add functions to the JS Object.

For example primevue has a menu structure with functions in the Object:

const items = {
        label: 'New',
        icon: 'pi pi-plus',
        command: (event) => {
            // event.originalEvent: Browser event
            // event.item: Menuitem instance
        }
    }
;

Nice, but what if you want this data provided from DB via php...?

this works:

const func = 'items.command = function(event){ console.log(event) }'
eval(func)

So we could do something with this. This means doing something with the JSON when it is loaded into Javascript and turn some strings into functions with eval. This is of course a security risk. But somehow we'll need to change the JSON into a JS Object.

Better maybe is to not have info of the end platform in the DB. The solution I'm using is that I'm pre-defining what commands can be added to the JS Object and use codes to tell the reader what I want added.

switch (add) {
    case '[login]':
        items.command: () => {
            auth.destroyToken()
            this.$router.push({ name: 'login' })
        }
    break;
}

Not as "free", but for security that's a good thing.

Altri suggerimenti

JSON does not allow function definition for security and compatibility reasons, and as a proof, I don't see any other way than storing a string and use a form of eval (that is using eval or creating a Function with a body). I'd strongly recommend not to do it though.

In order for it to be JSON, and for any JSON library to work with it, no, it is not possible.

JSON is a language and platform independent, portable data serialization scheme and, therefore, has a limited set of things which can be represented within. Functions, given that they are language and platform specific, are not portable.

It is possible to use PHP to "manually" output a JavaScript object which has methods:

<?php
    $foo = '{name: 5, value: function () { return "hi";}}'
?>
var js_object = <? echo $foo; ?>;

This is messy and error prone, however, and would not be JSON.

You can't add the function as value, but you can add the return value of the function as value of the json object. So in the example you gave, the value will just be "hi" (if you type return correctly).

I don't see the reason for adding a function into a JSON object, because you can probebly do the same with JQuery and if you can't then use AJAX to get results from the serverside.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top