SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Getting "Unexpected token ':'" from a simple JSON string

    Code:
    var info = eval(req.responseText);
    
    // req.responseText = {id: 2, title: "KVillains", description: "KVillains are an American punk rock band from Knoxville, TN and signed with Silverstone Records in Los Angeles, CA.", url: "http://kvillains.com"}
    
    alert(info.title[0]._text);
    The JSON is pulled in just fine, but when I try to access the information I get the unexpected token error.

    Any ideas?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Well this would result in the title being shown - does that help?

    Code javascript:
    req = {};
    req.responseText = {id: 2, title: "KVillains", description: "KVillains are an American punk rock band from Knoxville, TN and signed with Silverstone Records in Los Angeles, CA.", url: "http://kvillains.com"}
     
    info = req.responseText;
    alert(info.title);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Well this would result in the title being shown - does that help?

    Code javascript:
    req = {};
    req.responseText = {id: 2, title: "KVillains", description: "KVillains are an American punk rock band from Knoxville, TN and signed with Silverstone Records in Los Angeles, CA.", url: "http://kvillains.com"}
     
    info = req.responseText;
    alert(info.title);
    I get "undefined" with the code above.

  4. #4
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found doing the eval() with parenthesis properly builds the object.

    Code:
    var info = eval('(' + req.responseText + ')');

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by NokX View Post
    I found doing the eval() with parenthesis properly builds the object.

    Code:
    var info = eval('(' + req.responseText + ')');
    When it comes to converting JSON content, I highly recommend using json2.js from https://github.com/douglascrockford/JSON-js
    The reason why I recommend it is that some web browsers currently provide the same parsing functionality for JSON, and the above library is only used if the web browser doesn't already have the ability to convert JSON content.

    So, this way you can more safely convert the content, with:

    Code javascript:
    var info = JSON.parse(req.responseText);

    If the web browser doesn't know how to do JSON.parse, the json2.js file nicely adds that capability for you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

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
  •