0

Having an array

`Array
(
    [0] => Array
        (
            [operative] => 2
            [start] =>  01:00:00
            [end] =>  13:00:00
            [color] => 543939
        )

    [1] => Array
        (
            [operative] => 3
            [start] =>  08:00:00
            [end] =>  09:00:00
            [color] => 52e612
        )

)

`

need to change this into JSON so I use json_encode(), I get a string
[{"operative":"2","start":" 01:00:00","end":" 13:00:00","color":"543939"},{"operative":"3","start":" 08:00:00","end":" 09:00:00","color":"52e612"}]

but when I am using JSON into JavaScript by function $.parseJSON() or JSON.parse(), both are giving me not defined.

ok I have used it like this

options1 = '[{"operative":"2","start":" 01:00:00","end":" 13:00:00","color":"543939"},{"operative":"3","start":" 08:00:00","end":" 09:00:00","color":"52e612"}]'
options1 = $.parseJSON(options1);
alert($.param(options1));

But alert is showing me undefined=&undefined=

7
  • 2
    give us your javascript code. Commented Aug 9, 2013 at 8:15
  • did you surround it by quotes? Commented Aug 9, 2013 at 8:16
  • Make sure that the data you retrieve is, in fact, the json string and nothing more than that (console.log() it). Your json itself is valid, so $.parseJSON( data ) should return an array if you are passing that string as an argument. Commented Aug 9, 2013 at 8:19
  • var data = <?= json_encode($data) ?>; Commented Aug 9, 2013 at 8:23
  • I don't think alert takes 2 parameters Commented Aug 9, 2013 at 8:23

5 Answers 5

4

If you are using $.parseJSON() -function, the parseable data should be string:

var json = '[{"operative":"2","start":" 01:00:00","end":" 13:00:00","color":"543939"},{"operative":"3","start":" 08:00:00","end":" 09:00:00","color":"52e612"}]';

console.log($.parseJSON(json));
Sign up to request clarification or add additional context in comments.

Comments

2

It looks like you're echo-ing that json encoded string into your javascript. You can do this, and just leave the quotes out:

var options = <?= json_encode($array); ?>;

JSON is an acronym for JavaScript Object Notation. Echoing it into a JS script results in valid javascript arrays and object literals. No need to parse it at all.

Comments

1

Until you show your code I am assuming that this is what you did:

var object = $.parseJSON(<?php echo json_encode($array)?>);

and this wont work because parseJSON expects a string as a parameter.

so the solution will be:

var object = $.parseJSON('<?php echo json_encode($array)?>');

Note that it is surrounded by quotes.

1 Comment

But... why parse it if JSON is actually a JavaScript code? Just do var data = <?= json_encode($data) ?>;, no parsing needed!
0

JSON.parse() expects the argument to be a string. Make your JSON data a string by surrounding it with 'single-quotes' (because you've used "double quotes" inside the JSON already)

1 Comment

Or leave the quotes, and don't parse valid JSON. JavaScript Object Notation is valid JavaScript, just echo it unquoted!
0

Use parseJSON()

done(function (jsonresponse) {
var obj = $.parseJSON(jsonresponse);        
console.log(obj);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.