Search code examples
phpjqueryjsonstringify

I am using stringify to post an object to php in php even integers are string how can i fix this?


I am using stringify to post an object to php,

In php i use json_decode($object,true) to get the object back into a object for and not a string.

However the values stay strings even for integers.

My question is there a way to decode it to it's original form ?

ints to ints. string to stings ..

Thanks


Solution

  • Not sure what you're getting at:

    php > var_dump(json_decode(42)); // decoding a PHP int
    int(42)
    php > var_dump(json_decode('42')); // decoding a PHP string
    int(42)
    php > var_dump(json_decode('"42"')); // decoding a json string, containing a number
    int(42)
    php > var_dump(json_decode('foo')); // decoding invalid json
    NULL
    php > var_dump(json_decode('"foo"')); // decoding a valid string
    string(3) "foo"
    php > var_dump(json_decode('{"42":"foo"}')); // json object
    object(stdClass)#1 (1) {
      ["42"]=>
      string(3) "foo"
    }