25

I have a JavaScript array that, among others, contains a URL. If I try to simply put the URL in the page (the array is in a project involving the Yahoo! Maps API) it shows the URL as it should be.

But if I try to do a redirect or simply do an 'alert' on the link array element I get:

function(){return JSON.encode(this);}

As far as I see it this is because the browser does an JSON.encode when it renders the page, thus the link is displayed OK. I have tried several methods to make it redirect (that's what I want to do with the link) correctly (including the usage of 'eval') but with no luck.

After following some suggestions I've run eval('(' + jsonObject + ')') but it still returns the same output.

So how's this done ?

Brayn
  • 1,766
  • 10
  • 27
  • 33
  • It's not clear what you mean... can you post more sample code? The actual array contents and the redirect call would be very helpful. – Dylan Beattie Oct 08 '08 at 12:14

7 Answers7

44
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

See the jQuery API.

kaiser
  • 21,817
  • 17
  • 90
  • 110
matija kancijan
  • 1,205
  • 10
  • 11
9

Suppose you have an array in PHP as $iniData with 5 fields. If using ajax -

echo json_encode($iniData);

In Javascript, use the following :

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "ajaxCalls.php",
            data: "dataType=ini",
            success: function(msg)
            {
                var x = eval('(' + msg + ')');
                $('#allowed').html(x.allowed);              // these are the fields which you can now easily access..
                $('#completed').html(x.completed);
                $('#running').html(x.running);
                $('#expired').html(x.expired);
                $('#balance').html(x.balance);
            }
        });
    });
</script>
5

If you get this text in an alert:

function(){return JSON.encode(this);}

when you try alert(myArray[i]), then there are a few possibilities:

  • myArray[i] is a function (most likely)
  • myArray[i] is the literal string "function(){return JSON.encode(this);}"
  • myArray[i] has a .toString() method that returns that function or that string. This is the least likely of the three.

The simplest way to tell would be to check typeof(myArray[i]).

Joel Anair
  • 13,832
  • 3
  • 31
  • 36
  • I doubt it that is a literal string. Most likely is a function. I have tried to "eval('(' + jsonObject + ')')" but still it returns the same output as before... – Brayn Oct 08 '08 at 15:58
  • Yeah, would imagine that passing a function to eval() would call that function's toString method, returning a string that would eval back to that function itself. So that makes sense. – Joel Anair Oct 08 '08 at 18:27
2
eval('(' + jsonObject + ')')
Kon
  • 27,113
  • 11
  • 60
  • 86
1

JSON decoding in JavaScript is simply an eval() if you trust the string or the more safe code you can find on http://json.org if you don't.

You will then have a JavaScript datastructure that you can traverse for the data you need.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • As I was saying I tried to 'eval' the link element but then it returned 'undefined'. The data in the array comes form parsing a XML, do you think that it can have something to do with that ? – Brayn Oct 08 '08 at 12:23
1

If the object element you get is a function, you can try this:

var url = myArray[i]();
Leonel Martins
  • 2,713
  • 1
  • 21
  • 24
-1

I decode JSON this way:

eval( 'var from_json_object = ' + my_json_str + ';' );
pirogtm
  • 90
  • 7