Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

XHR POST, JSON Array and a Tableview

This might be fairly easy for some, but I've been pulling my hair out over this the last 2 days.

I'm doing a post with the httpclient, which returns a response (this.responseText) as a JSON Array. I need to parse this array and populate a tableview with its data.

Here is the return data I'm getting

({"success":"true","results":[{"0":"Operation5","clan":"Operation5","1":"1","rank":"1","2":"6669","kills":"6669","3":"3","death":"3"}]})

I've tried looking through a lot of the JSON issues people have had on here, as well as looking through the kitchen sink examples. None of them seem to do the trick.

Im currently testing in Android 2.1, with the Titanium 1.4 SDK

Any examples, advice or code snippets you guys can share?

— asked August 6th 2010 by Chris Nelson
  • json
  • json.parse
  • tableview
0 Comments

3 Answers

  • Accepted Answer

    The response text contains "()" ? this makes it an invalid JSON string, try to remove it from the string and parse the result with JSON.parse

    {"success":"true","results":[{"0":"Operation5","clan":"Operation5","1":"1","rank":"1","2":"6669","kills":"6669","3":"3","death":"3"}]}
    

    so without parenthesis.

    — answered August 6th 2010 by Dan Tamas
    permalink
    0 Comments
  • It would be a good idea to post what platform you are using, and a sample of the data return. Otherwise, you aren't likely to get much helps with post like this.

    — answered August 6th 2010 by Daniel Lim
    permalink
    1 Comment
    • thanks, I updated the post

      — commented August 6th 2010 by Chris Nelson
  • Ran on 2.1 android.

    var b = JSON.parse('{"success":"true","results":[{"0":"Operation5","clan":"Operation5","1":"1","rank":"1","2":"6669","kills":"6669","3":"3","death":"3"}]}');
    
    Ti.API.info(b.success);
    Ti.API.info(b.results[0].clan);
    Ti.API.info(b.results[0].rank);
    Ti.API.info(b.results[0].kills);
    
    — answered August 6th 2010 by Daniel Lim
    permalink
    2 Comments
    • I shouldn't be parsing this.responseText? The results will be different for every user.

      — commented August 6th 2010 by Chris Nelson
    • Well, i just test and parse your sample. It's your end to try the dynamic responseText. :)

      As long as the format remains the same, you should be ok.

      — commented August 6th 2010 by Daniel Lim
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.