Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to JSON decode array elements in JavaScript?

Tags:

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 ?

like image 311
Brayn Avatar asked Oct 08 '08 12:10

Brayn


People also ask

How do you parse an array in JSON?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

Does JSON parse return an array?

When using the JSON. parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.

Can you JSON Stringify an array?

Stringify a JavaScript ArrayIt is also possible to stringify JavaScript arrays: Imagine we have this array in JavaScript: const arr = ["John", "Peter", "Sally", "Jane"]; Use the JavaScript function JSON.

How does JSON parse work?

How Does JSON Parse Work? The JSON parse function takes data that's in a text format and then converts it into JavaScript. Usually, these take the form of JavaScript objects, but the parse function can be used directly on arrays as well.


1 Answers

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

See the jQuery API.

like image 176
matija kancijan Avatar answered Sep 21 '22 17:09

matija kancijan