Titanium Community Questions & Answer Archive

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

is the JSON.parse included in Ti broken?

i tried to get array of objects from server and populate the result to table view,…

here's an example of the response text:

[{name: John, group: [foo, bar, baz]},
{name: Bob, group: [abc, def, ghi]}]

(i know it's missing all the quotes, but it's just an example, the real data is valid, i checked and have use it for browser extensions)

here's the result of JSON.parse

({name= John, group = (foo, bar, baz)},
{name= Bob, group = (abc, def, ghi)}; )

notice the square brackets… ergo the loop breaks and when I saved to flat file (via JSON.stringify) and reload the tableview, the app just breaks if i tap on anything…

update

i've figured it out, the loop isn't actually broken but JSON.parse is… i originally wanted to save the flat file to be a proper TableView data, so on 2nd round app launch i just need to parse the file and load the TableView… so, i just reformat after reading the file atm…

thanks anyway…

— asked December 1st 2010 by soemarko ridwan
  • iphone
  • json
  • parse
0 Comments

2 Answers

  • Are you testing under iOS or Android?

    If Android, I noticed a comment in a code section of another project which says in part:

    >Include the json2.js JSON definition, because Titanium's Android JSON function doesn't stringify arrays properly as of 1.4.2.

    See the redux project at https://github.com/dawsontoth/Appcelerator-Titanium-Redux

    Try it (or what it does) to see if that solves it for you. Or you could try the nightly 1.5.x beta builds, as many Android fixes will be in 1.5.x however the nightly builds are not yet considered stable. See http://developer.appcelerator.com/doc/mobile/get_continuous_builds

    — answered December 1st 2010 by Doug Handy
    permalink
    0 Comments
  • JSON parse works fine as long as I know. Maybe posting some code where you are actually using the result will help find an error ( if any:) ).
    And a small piece of the json reply.

    — answered December 1st 2010 by Dan Tamas
    permalink
    1 Comment
    • the real data

      the response

      [
          {
              "name": "home pc",
              "tabs": [
                  {
                      "title": "8tracks | Handcrafted internet radio",
                      "url": "http://8tracks.com/" 
                  },
                  {
                      "title": "Google",
                      "url": "http://www.google.com.au/" 
                  } 
              ] 
          },
          {
              "name": "macbook",
              "tabs": [
                  {
                      "title": "Gmail - Your Appcelerator question was (possibly) answered! - soemarko@gmail.com",
                      "url": "https://mail.google.com/mail/u/0/?ui=2&shva=1#mbox/12ca24ef876c3f21" 
                  },
                  {
                      "title": "Google Reader (1000+)",
                      "url": "http://www.google.com/reader/view/#stream/user%2F11202412486240339008%2Flabel%2Flol" 
                  },
                  {
                      "title": "Appcelerator Developer Center - API for Titanium.UI (version 1.4)",
                      "url": "http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI-module" 
                  },
                  {
                      "title": "JSONLint - The JSON Validator.",
                      "url": "http://www.jsonlint.com/" 
                  },
                  {
                      "title": "Appcelerator Developer Center - is the JSON.parse included in Ti broken?",
                      "url": "http://developer.appcelerator.com/question/84931/is-the-jsonparse-included-in-ti-broken" 
                  } 
              ] 
          }
      ]
      

      parsed

      ({
          name = "home pc";tabs = ({
              title = "8tracks | Handcrafted internet radio";url = "http://8tracks.com/"; 
          },
          {
              title = Google;url = "http://www.google.com/"; 
          });
      },
      {
          name = macbook;tabs = ({
              title = "Gmail - Your Appcelerator question was (possibly) answered! - soemarko@gmail.com";url = "https://mail.google.com/mail/u/0/?ui=2&shva=1#mbox/12ca24ef876c3f21"; 
          },
          {
              title = "Google Reader (1000+)";url = "http://www.google.com/reader/view/#stream/user%2F11202412486240339008%2Flabel%2Flol"; 
          },
          {
              title = "Appcelerator Developer Center - API for Titanium.UI (version 1.4)";url = "http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI-module"; 
          },
          {
              title = "JSONLint - The JSON Validator.";url = "http://www.jsonlint.com/"; 
          },
          {
              title = "Appcelerator Developer Center - is the JSON.parse included in Ti broken?";url = "http://developer.appcelerator.com/question/84931/is-the-jsonparse-included-in-ti-broken"; 
          });
      })
      

      — commented December 1st 2010 by soemarko ridwan
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.