Irish SEO,  Marketing & Webmaster Discussion

 

JSON Headache

This is a discussion on JSON Headache within the Coding Help forums, part of the Webmaster Help category; Hey, I'm having a problem with some JSON (specifically manipulating JSON retrieved the Google Gears httprequest function) - I'm retrieving ...


Go Back   Irish SEO, Marketing & Webmaster Discussion > Webmaster Help > Coding Help

Register Forum Rules FAQDonate Calendar Search Today's Posts Mark Forums Read

  #1 (permalink)  
Old 17-07-2008, 04:50 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 85
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default JSON Headache

Hey,

I'm having a problem with some JSON (specifically manipulating JSON retrieved the Google Gears httprequest function) - I'm retrieving JSON data which I've tried to parse using eval():
Code:
var response = request.responseText;  // httprequest response
var jsRespObj = {};
jsRespObj = eval("(" + response + ")");
if(jsRespObj.results[0].error != null){
    alert('Hi');    // never gets to this line
}
On the line jsRespObj.results[0].error I'm getting an error thrown: "jsRespObj.results is undefined".

It looks like the jsRespObj variable holds a String value, rather than a JSON object that can be manipulated...but I haven't any idea why.

The JSON data is valid, so printed after parse it looks like:
Code:
{"results":[{"cat_id":"1","cat_name":"A cat name","treat_group":[],"error":"Test"},{"cat_id":"2","cat_name":"Another Cat Name","treat_group":[],"error":"Test"}]}
Any suggestions on what the problem might be? I've been staring at it for a while and might be missing something obvious now!
__________________
Not Work: My blog
Work: Website Design Galway
A little app: Twiteye.com (Application, software & service ideas from Twitter)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 17-07-2008, 05:09 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 85
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
davkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud ofdavkell has much to be proud of
Default Sorted :)

Have to love that - I've been staring at it for ages, and one minute after I posted above, I tripped over the solution.

The problem was (just in case anyoune suffers the same) - I was passing the JSON response from one function to another. To do this I should have parsed it both before and after sending.

Code:
function getAll(){
    var request = google.gears.factory.create('beta.httprequest');
    request.open('GET', '/find/gearssetup/');
    
    request.onreadystatechange = function() {
    
      if (request.readyState == 4) {
    try{
            var response = request.responseText;
            var r = {};
            r = eval("(" + response + ")");    // ** Needed to Parse here too
            processAll(r);            
        }// try
        catch(ex){
            log("Error: " + ex.message);
        } // catch
      }    // readystate
    };    // onstatechange
    
    request.send();
} // getAll()

function processAll( response ){

     var jsResp = eval("(" + response + ")");    
....
....
}
Dave.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
headache, json

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Sponsored links

Pepperjam Network
Paid On Results www.zanox.com


All times are GMT +1. The time now is 12:28 PM.


Powered by: vBulletin Version 3.8.2, Copyright ©2000 - 2009, Jelsoft Enterprises Limited.
Hosted in Ireland by Blacknight - Test your ISP |Irish Hosting Directory| Armchair.ie|Logo by Eden Web Design|Avatars by Afterglow |Latest Blog Entries | VPS HostingAd Management by RedTyger

Search Engine Friendly URLs by vBSEO 3.3.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51