Results 1 to 9 of 9
  1. kmberntsen's Avatar
    Posts
    9 Posts
    Global Posts
    10 Global Posts
       #1  
    I'm getting the followin error when I try to parse the JSON response I'm getting back from a web service.

    Can any one point me in a direction to try debugging this. I've tried loading parsing in via my browser and I don't get any errors.

    Uncaught SyntaxError: String '(undefined)' is not valid JSON, native json.jsjsjs:$32$

    The json is attached.
    GroupHiearchy.txt


    Here's the code block where I'm trying to parse the string.
    Code:
    var responseText = request.responseText;
    Mojo.Log.info(responseText); //This Works I get my string
    var retVal;
    try {
        retVal = Mojo.parseJSON(responseText);
    } catch (e) {
        Mojo.Log.error(e);
    }
  2. kmberntsen's Avatar
    Posts
    9 Posts
    Global Posts
    10 Global Posts
       #2  
    Is there any one that can give me a hint as to what might be going on? Is the response to big? Is it the nested JSON within JSON?

    I have wondered does the JSON parse routine parse only the first level or is it recursive?
  3. tc600's Avatar
    Posts
    305 Posts
    Global Posts
    306 Global Posts
    #3  
    Do you have control over the service? I'd start by trying a much smaller JSON object - one with a JSON object within another JSON as one test and another JSON without the embedded JSON as another test. Either way, I test with a response that's much, much smaller the 277K that you currently have.
  4. #4  
    Since you're getting it from an AJAX.Request, I would recommend you simply enable JSON parsing from the download URL and parse it.

    Code:
    var response = request.responseText.evalJSON();
    instead of:
    Code:
    var responseText = request.responseText;
    May or may not make a difference. I just recommend you do that and evaluate the JSON immediately after receiving it.

    Or you can just call request.responseJSON and see if it has the JSON already evaluated.
    Arthur Thornton

    Former webOS DevRel Engineer at Palm, HP, and LG
    Former webOS app developer (built Voice Memos, Sparrow, and several homebrew apps and patches)
    Former blogger for webOS Nation and webOS Roundup
  5. kmberntsen's Avatar
    Posts
    9 Posts
    Global Posts
    10 Global Posts
       #5  
    @tc600 unfortunately no I don't have control of the service or I would have tried something much simpler.

    @arthur Your suggestion worked. Not sure what exactly was in play but as long as it worked. I now get the object back and can work on processing it.

    Thanks
  6. #6  
    Quote Originally Posted by kmberntsen View Post
    @tc600 unfortunately no I don't have control of the service or I would have tried something much simpler.

    @arthur Your suggestion worked. Not sure what exactly was in play but as long as it worked. I now get the object back and can work on processing it.

    Thanks
    Glad it worked for you.
    Arthur Thornton

    Former webOS DevRel Engineer at Palm, HP, and LG
    Former webOS app developer (built Voice Memos, Sparrow, and several homebrew apps and patches)
    Former blogger for webOS Nation and webOS Roundup
  7. DrewPre's Avatar
    Posts
    818 Posts
    Global Posts
    829 Global Posts
    #7  
    not sure if this applies, but I was reading this....

    The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.

    To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
    if this, Mojo.Log.info(responseText);, works then maybe, the value in responseText, could be massaged into proper and valid JSON {'key1':'value1', 'key2':'value2', ...'keyn':'valuen'}, format....and then JSON.parse [instead of Mojo.parseJSON] could be used.

    Palm Pre Backup Utility...done!
    Locate Pre....done!
  8. #8  
    Quote Originally Posted by DrewPre View Post
    not sure if this applies, but I was reading this....



    if this, Mojo.Log.info(responseText);, works then maybe, the value in responseText, could be massaged into proper and valid JSON {'key1':'value1', 'key2':'value2', ...'keyn':'valuen'}, format....and then JSON.parse [instead of Mojo.parseJSON] could be used.
    I assume you got that from a site not specifically talking about webOS? If so, I believe it was saying using eval() is unsafe -- which it isn't.

    I am not sure what method evalJSON uses, but I would hope it is a json parser moreso than just a cover method for eval().

    Hopefully it parses the JSON (to make it safer) and eval()'s it.
    Arthur Thornton

    Former webOS DevRel Engineer at Palm, HP, and LG
    Former webOS app developer (built Voice Memos, Sparrow, and several homebrew apps and patches)
    Former blogger for webOS Nation and webOS Roundup
  9. kmberntsen's Avatar
    Posts
    9 Posts
    Global Posts
    10 Global Posts
       #9  
    I'll also add that I had no problems parsing the JSON using JSON.parse on my desktop browser. I was able to parse it and access the values all the way down.

    The problem only came in when I tried storing the result in a variable and then tried parsing it. Its possible that it had already been parsed since the return header said it was JSON and I was getting an error because I was trying to parse an object and not a string.

Posting Permissions