1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Funny jsonparse error

Discussion in 'jQuery' started by sarahk, Sep 4, 2015.

  1. #1
    I'm getting this in my console

    upload_2015-9-4_19-23-24.png

    when I run

    
    dataType: "json",
      success: function (data, textStatus) {
       
      var json = $.parseJSON(data);
      console.log(json); 
    Code (markup):
    If I look at the network tab in my browser I can see that the browser itself can read the json just fine. If I copy and paste the json into jsonlint it validates. There are no extra characters, no spaces or blank anythings.

    upload_2015-9-4_19-27-12.png

    Any ideas what might be wrong?
     
    sarahk, Sep 4, 2015 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Can you post the raw unparsed JSON?
     
    digitalpoint, Sep 4, 2015 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #3
    Sure can :)
    {"membership_id":"11","association_id":"2"}
    Code (markup):
    console.log(data) gives me this
    upload_2015-9-5_14-52-23.png

    I figure since it's a simple object I'll just call data.membership_id and be done with it.
     
    Last edited: Sep 4, 2015
    sarahk, Sep 4, 2015 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    Don't know if it's applicable or not but you can use stringify. Something like

    JSON.parse(JSON.stringify({"membership_id":"11","association_id":"2"}));

    Not claiming I am familiar with json, just saw a similar example somewhere suggesting doing this if that error occurs.
     
    qwikad.com, Sep 4, 2015 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Do you still get the error if you use the REAL JSON object instead of the jquerytard bs? If not, use the real modern parser directly then use something like JSON3 as a polyfill for legacy IE.

    ... or just use the polyfill provided at MDN, though it could use a few efficiency tweaks.
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

    Gah, those multiple VAR, ELSE after RETURN and multiple IF on the same value REALLY makes me want to pimp-slap somebody. Used to be you could trust MDN dev's to have some clue what they are doing. No more it seems... :(
     
    deathshadow, Sep 5, 2015 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    It works when I try it manually...

    var jsonData = '{"membership_id":"11","association_id":"2"}';
    console.log($.parseJSON(jsonData));
    VM324:2 Object {membership_id: "11", association_id: "2"}
    Code (markup):
    If your "console.log(data)" function was already saying it's an object before running parseJSON(), then it looks to me like somewhere it was already decoded into an object. The key here I think is this portion of your AJAX request:

    dataType: "json"
    Code (markup):
    You are telling jQuery that it's JSON coming back and it will automatically decode it for you... so in the end you are trying to decode something that's already decoded.
     
    digitalpoint, Sep 6, 2015 IP
    HuggyStudios, ryan_uk and deathshadow like this.
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #7
    D'oh, that's what comes of working late!
    New week, fresh brain, it's all going to be great
     
    sarahk, Sep 6, 2015 IP