SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to structure JSON data?

    I'm trying to send my first JSON object to a client, like so:
    PHP Code:
    /* http://localhost/poll/index.php */

    header('Content-type: application/json');

    echo 
    "{
      {'type'    : 'radio', 
       'question': 'How do you feel?',
       'options' : ['Happy', 'Sad']
      },
      {'type'    : 'radio',
       'question': 'What is you favorite color?',
       'options' : ['red', 'yellow', 'blue']
      },
      {'type'    : 'textarea',
       'question': 'What is your opinion on pencils?'
      }
    }"

    And on the clientside:
    Code:
    var Poll = Poll || {'id' : 123};
    
    Poll.fetchJSON = function() {
      $.getJSON("http://localhost/poll/index.php?id=" + Poll.id + "&callback=?",
        function (data) {
          alert(data);
        });
    }
    
    $(document).ready(function() {
      Poll.fetchJSON();
    });
    But when I run it, nothing happens. Firebug tells me:
    invalid label: {'type' : 'radio', \n
    http://localhost/poll/index.php?id=1...=1234302394377
    Line 2
    So I put all the JSON date in one long string (removing \n) but this didn't help - I then got no error, but nothing happened either. What am I doing wrong?

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    first off, why are you reinventing the wheel? this is the kind of case where you should be using a library.

    second, you have 3 top-level elements, but none of them have labels. that's why you're getting an error about an invalid label.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by reinventing the wheel? What library should I use? I don't think I am reinventing...planning on using jquery functions to parse the data...

    About the JSON structure: could you help me with it? I've tried
    PHP Code:
    echo "{
      'question 1' : {'type'    : 'radio', 
                      'question': 'How do you feel?',
                      'options' : ['Happy', 'Sad']
      },
      'question 2' : {'type'    : 'radio',
                      'question': 'What is you favorite color?',
                      'options' : ['red', 'yellow', 'blue']
      },
      'question 3' : {'type'    : 'textarea',
                      'question': 'What is your opinion on pencils?'
      }
    }"

    But that doesn't work either. What am I missing?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    json_encode() a multidimensional associative array.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I now used json encode:
    PHP Code:
    header('Content-type: application/json');

    $poll = array(
      
    => array(
        
    'type' => 'radio'
        
    'question' => 'How do you feel?',
        
    'options' => array('happy''sad')
      ),
      
    => array(
        
    'type' => 'radio',
        
    'question' => 'What is you favorite color?',
        
    'options' => array('red''yellow''blue')
      ),
      
    => array(
        
    'type' => 'textarea',
        
    'question' => 'What is your opinion on pencils?'
      
    )
    );

    echo 
    json_encode($poll); 
    But Firebug still gives me an "invalid label" error. I don't understand...

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var myVariableName = {...stuff...};

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2008
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Code:
    var myVariableName = {...stuff...};
    If I type that into Firebug and run it, it doesn't give an error..when it comes from the server, it does?!

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    You're specifying a callback in the URL ("callback=?") but not providing that functionality in the PHP. Either, remove that callback argument (if the JSON will be coming from the same domain) from the URL or implement the callback in the PHP.

    Removing the callback
    Code JavaScript:
    // Change this
    $.getJSON("http://localhost/poll/index.php?id=" + Poll.id + "&callback=?",
    // to this
    $.getJSON("http://localhost/poll/index.php?id=" + Poll.id,

    or, implement the callback
    Code PHP:
    $json = json_encode($poll);
     
    if ( ! empty($_GET['callback']) && preg_match('/^[\w.]+$/Di', $_GET['callback']))
    {
    	$json = $_GET['callback'].'('.$json.');';
    }		
     
    echo $json;
    exit;
    Salathe
    Software Developer and PHP Manual Author.


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
  •