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 ...
| |||||||
| Register | Forum Rules | FAQ | Donate | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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
}
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"}]}
__________________ Not Work: My blog Work: Website Design Galway A little app: Twiteye.com (Application, software & service ideas from Twitter) |
| ||||
| 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 + ")");
....
....
}
|
| Tags |
| headache, json |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| | ![]() | |||
| | ||||