Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have something like this...

["a","b","c"]

I got it from PHP array using json_decode(). I'm very weak in JavaScript, but I need to get array in Javascript, so I can use it with jQuery UI -> Autocomplete like this...

source: [ 'a', 'b', 'c' ]

Is it possible to do with only Javascript or you need to have some Javascript library to use JSON?

share|improve this question
 
Consider: <script>var myObj = <?= encode_your_php_object_to_json(...) ?>;</script> and what it implies. See json.org for more details. –  user166390 Jan 2 '11 at 7:15
add comment

2 Answers

up vote 5 down vote accepted

but I need to get array in Javascript, so I can use it with jQuery UI -> Autocomplete

Check out jQuery.parseJSON function.

share|improve this answer
add comment

Hey, You can do it only with javascript:

var arrayToUse = ["a","b","c"];

JSON stands for Javascript Object Notation and it is a standard notation for javascript objects. You do not need any special library to use it. Example,

If you want to create an object in javascript you would do something like:

var person = {
 name : 'somename',
source : arrayToUse
};

This is a javascript object in JSON notation

share|improve this answer
3  
"JSON stands for Javascript Object Notation and it is a standard notation for javascript objects." That's a bit misleading. The standard notation for JavaScript objects is JavaScript object liternal notation. JSON is a subset of object literal notation. –  T.J. Crowder Jan 2 '11 at 7:11
1  
JSON is more restrictive than JavaScript -- it is a valid subset. For instance, all keys must be quoted to be valid JSON. The grammar "tracks" can be followed at json.org –  user166390 Jan 2 '11 at 7:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.