Irish SEO,  Marketing & Webmaster Discussion
 
Chitika Malls
 

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


Notices

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 17-07-2008, 04:50 PM
Coder
 
Join Date: Jul 2007
Location: Galway
Posts: 43
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!
__________________
Website Design Galway | Personal Site
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: 43
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.
__________________
Website Design Galway | Personal Site
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

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
Forum Jump


All times are GMT +1. The time now is 04:57 AM.


Powered by: vBulletin Version 3.7.2, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.

Search Engine Friendly URLs by vBSEO 3.2.0