SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array to json string to array

    Hello,

    I'm having a PHP array which I json_encode. This array is used by a .js file.

    Here's how I do so far:

    Code JavaScript:
    var myResult = JSON.parse(arrayJsonEncoded);
     
    //outputs: result1,result2,result3								
    alert(myResult);
     
    myResultArray = myResult.split(",");
     
    //would expect result2 but... nothing
    alert(myResultArray[1]);

    How do you retrieve an array from a json string?

    Cheers.


  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rhgiant View Post
    How do you retrieve an array from a json string?
    Head along to json.org and at the end of the page you will see code libraries for a large number of languages.

    For JavaScript, use json2.js
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,799
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    For JavaScript, use json2.js
    And if you don't need to support IE7 and earlier then you can just use the JSON.stringify and JSON.parse methods without needing that script to define them as all modern browsers now have that JSON object built in (which is why that script first checks whether or not the JSON object exists - so that it can avoid overwriting the built in object that most browsers now have that provides the same functionality).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •