2

Is it possible to read these strings (was converted to strings from json format) into php arrays?

jQuery:

  $.ajax({

            type: 'GET',
            url: 'index.php?task=search&wtd=ul',
            data: $('#fupdate').serialize()+'&derulo='+$('#getdbresult').val(),
            beforeSend: function(){
                $('.the-modal').html('Updating...');
            }, 
            success: function(d){
                $('.the-modal').html(d);
                console.log(d);
            }

    });

PHP:

var_dump(json_decode("{$_REQUEST['derulo']}", true));

VALUES:

string(379)"[{\"target_detail_id\":\"66351031\",\"first_name\":\"Dorothy\",\"last_name\":\"Smith\",\"company\":\"Active Money Now\",\"sic_code\":\"\",\"position\":\"\",\"target\":\"1300572544\",\"website\":\"\",\"email\":\"[email protected]\",\"email_status\":\"\",\"country\":\"Australia\",\"city\":\"Broken Hill\",\"postal_code\":\"2880\",\"address\":\"Po Box 41\",\"note\":\"\"}]" 

RESULT:

NULL

I tried using json_decode but nothing appears. Also looked at using implode to separate strings accordingly. But is there any other way to this?

EDIT:

It works with: $object = (json_decode(stripslashes($_REQUEST['derulo'])));

6
  • 1
    Maybe you should use json_decode?... Commented Jun 5, 2014 at 8:25
  • I have rephrase the question, I was did use json_decode. typo error Commented Jun 5, 2014 at 8:27
  • If so, what means "nothing appears"? Did you try json_decode('...json...', true);? Commented Jun 5, 2014 at 8:29
  • Yes. Also tried that, but when print_r'ed nothing appears.. Commented Jun 5, 2014 at 8:30
  • 1
    Post your actuall function call and how you checked the result. The example is valid json, there is no reason why it shouldn't work. Commented Jun 5, 2014 at 8:30

2 Answers 2

4

You need to use json_decode ("Decodes a JSON string") instead of json_encode ("Returns the JSON representation of a value").

Assuming your input is correct (and the example presented by you is), the return value will be an object representing the JSON data. You can also get the result as an array (see the docs for parameter reference).


EDIT: There must be another problem with your code, please post your complete code.
(My guess would be that you are not checking the returned value but expect json_decode to modify the parameter by reference and only check the parameter variable - that would explain why "nothing happens")

The following works:

(Ideone snippet)

PHP code:

<?php

$json = <<<JSON
[{"target_detail_id":"66351031","first_name":"Dorothy","last_name":"Smith","company":"Active Money Now","sic_code":"","position":"","target":"1300572544","website":"","email":"[email protected]","email_status":"","country":"Australia","city":"Broken Hill","postal_code":"2880","address":"Po Box 41","note":""}]
JSON;

$object = json_decode($json);
$array = json_decode($json, true);

var_dump($object);
var_dump($array);

output:

array(1) {
  [0]=>
  object(stdClass)#1 (15) {
    ["target_detail_id"]=>
    string(8) "66351031"
    ["first_name"]=>
    string(7) "Dorothy"
    ["last_name"]=>
    string(5) "Smith"
    ["company"]=>
    string(16) "Active Money Now"
    ["sic_code"]=>
    string(0) ""
    ["position"]=>
    string(0) ""
    ["target"]=>
    string(10) "1300572544"
    ["website"]=>
    string(0) ""
    ["email"]=>
    string(26) "[email protected]"
    ["email_status"]=>
    string(0) ""
    ["country"]=>
    string(9) "Australia"
    ["city"]=>
    string(11) "Broken Hill"
    ["postal_code"]=>
    string(4) "2880"
    ["address"]=>
    string(9) "Po Box 41"
    ["note"]=>
    string(0) ""
  }
}
array(1) {
  [0]=>
  array(15) {
    ["target_detail_id"]=>
    string(8) "66351031"
    ["first_name"]=>
    string(7) "Dorothy"
    ["last_name"]=>
    string(5) "Smith"
    ["company"]=>
    string(16) "Active Money Now"
    ["sic_code"]=>
    string(0) ""
    ["position"]=>
    string(0) ""
    ["target"]=>
    string(10) "1300572544"
    ["website"]=>
    string(0) ""
    ["email"]=>
    string(26) "[email protected]"
    ["email_status"]=>
    string(0) ""
    ["country"]=>
    string(9) "Australia"
    ["city"]=>
    string(11) "Broken Hill"
    ["postal_code"]=>
    string(4) "2880"
    ["address"]=>
    string(9) "Po Box 41"
    ["note"]=>
    string(0) ""
  }
}

EDIT2:

  1. You don't need quotes around the json_decode parameter, just use

    json_decode($_REQUEST['derulo'], true);
    

    But this is not what is causing problems (it's just inefficient, php has to parse another variable in a string).

  2. Your php snippet works, so you must be getting wrong data from the query. You can easily check for that with

    var_dump($_REQUEST['derulo']);
    

    You shouldn't be mixing sending data in both the url and over data, when using a GET request or switch to POST. I'd recommend letting jQuery take care of the serialization of the data, e.g.

    $.ajax({
        type: 'GET',
        url: 'index.php',
        data: {
          'task':'search',
          'wtd':'ul',
          'derulo':JSON.stringify($('#getdbresult').val())
        },
        beforeSend: function(){
            $('.the-modal').html('Updating...');
        }, 
        success: function(d){
            $('.the-modal').html(d);
            console.log(d);
        }
      });
    
Sign up to request clarification or add additional context in comments.

3 Comments

I have rephrase the question, I did use json_decode.
I think the $_REQUEST values is not enclosed as string. That's is why json_decode can't read the value as string
Tried your suggested jquery, the result now has string(379) at the beginning using var_dump will edit the result on the question
0

The code you attached to your question is already formatted in json. So you need to decode it with json_decode function. It returns an object, just typecast it to an array. Be aware that the json code is an array by itself (everything is inside square brackets), so we must refer to the first item ($item[0]).

$json = '[{"target_detail_id":"66351031","first_name":"Dorothy",...';
$items = json_decode($json);
$items_array = (array) $items[0];

Json_decode offers also an option to return automatically an array: json_decode($json, true).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.