Array in SetString

You must Login before you can answer or comment on any questions.

Hi all,

When i do this the result is 6 (good)

var inhouddatabase = ['a', 'b', 'c', 'd', 'e', 'f'];
var maxVal = inhouddatabase.length;
Ti.API.log('database:'+maxVal);
but when i do this the result is 44 (wrong) Any clue why this happends?
var inhouddatabases = ['a', 'b', 'c', 'd', 'e', 'f'];
Titanium.App.Properties.setString("database",inhouddatabases); 
var inhouddatabase = Ti.App.Properties.getString('database');
var maxVal = inhouddatabase.length;
Ti.API.log('database:'+maxVal);

8 Answers

Accepted Answer

Titanium.App.Properties.setString("database",JSON.stringify(inhouddatabases));
var db = JSON.parse(Titanium.App.Properties.getString("database"));

— answered 10 months ago by Alexander Bauer
answer permalink
1 Comment
  • Hi Alexander

    We started with that style solution but that only solves his local property save, does not use the new more suitable method, setObject in this case and also does not resolve the main problem which is how he accesses the actual data as he has an issue with in his PHP.

    — commented 10 months ago by Malcolm Hollingsworth

Well maybe you should use setList as it was intended instead of setString.

Hi Gerd

Use setList instead of setString.

This can take array information, without needing to change it from an array to anything else first, same is true when retrieving it.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
3 Comments
  • thanks both

    — commented 10 months ago by Gerd Koetje

  • Your welcome, please mark Stephens answer as correct as he beat me by a couple of minutes, remember marking questions as answered helps others who need the same info.

    — commented 10 months ago by Malcolm Hollingsworth

  • But Malcolm gave you more details...

    — commented 10 months ago by Stephen Feather

now i added an array of data to the var but he gives and error when i try to get it for a count

var inhouddatabase = Ti.App.Properties.getList('database');
var maxVal = inhouddatabase.length;
Ti.API.log('database:'+maxVal);
the error
[ERROR] {
    line = 68;
    message = "'undefined' is not an object (evaluating 'inhouddatabase.length')";
    name = TypeError;
    sourceId = 233238176;
    sourceURL = "file://localhost/Users/NBhosting/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/08F671E2-697A-4B5E-813C-4039509DA3B3/test.app/functies.js";
}

i filled database with data from php/json i parsed it with var content = JSON.parse(this.responseText); and added the result to database

my returned data from php looks like this

{"0":{"pid":"2","profielnaam":"garett","leeftijd":"22","foto":"1284251614yzyms.jpg"},"1":{"pid":"12","profielnaam":"francois","leeftijd":"24","foto":"Francois23.jpg"},"2":{"pid":"23602","profielnaam":"Robin","leeftijd":"19","foto":"1292933541hdstv.jpg"},"3":{"pid":"23599","profielnaam":"Stef30","leeftijd":"30","foto":""},"4":{"pid":"23087","profielnaam":"unexpected1","leeftijd":"48","foto":""},"5":{"pid":"23587","profielnaam":"rudie58","leeftijd":"53","foto":"1290706787jmnpf.jpg"},"6":{"pid":"23076","profielnaam":"monique","leeftijd":"29","foto":"1282915347rellf.jpg"},"7":{"pid":"24959","profielnaam":"thomaz","leeftijd":"34","foto":""},"8":{"pid":"870","profielnaam":"ixixon","leeftijd":"35","foto":"Ixixon.jpg"},"9":{"pid":"898","profielnaam":"jawsper","leeftijd":"32","foto":"jawsper.jpg"},"10":
*****
*****
more
*****
*****
{"aantal":5808}}

Hi Gerd

This might be because you swapped the array to an object (this may contain arrays) but not the same.

Try setObject in place of the current setList.

If that fails, validate your JSON using something like jsonlint.com, if that works post your code with the set property in it.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
2 Comments
  • Hi Gerd

    Are you still using length as a test? If so why as your object example does not start with an array, and as it is not a string it has nothing left it could count.

    Looking at your returned JSON data - it contains no single array element at all, your pho code has an error in it as the nodes you think are array elements are actually child notes with the value of "0", "1" etc.

    Fix that problem first and then you can run your app test again.

    Let me know how it goes

    — commented 10 months ago by Malcolm Hollingsworth

  • Guessing this is the most suitable answer then

    — commented 10 months ago by Malcolm Hollingsworth

done that , var seems to be an object now [INFO] database:[object Object]

how do i cont with an object?

var inhouddatabase = Ti.App.Properties.getObject('database');
var maxVal = inhouddatabase.length;
Ti.API.log('database:'+inhouddatabase);
Ti.API.log('database:'+maxVal);
returns undifined

— answered 10 months ago by Gerd Koetje
answer permalink
2 Comments
  • json seems valid

    — commented 10 months ago by Gerd Koetje

  • JSON may seem valid but that does not mean it is right!

    Notice there are no [ or ] symbols, that denote array elements

    — commented 10 months ago by Malcolm Hollingsworth

tryed to add

datadb:[
datahere
]
donest seem to work either

got it working, was missing [] in my output from php

— answered 10 months ago by Gerd Koetje
answer permalink
5 Comments
  • you can use json_encode(); within php, it will do all the json formatting for you. If you didnt already.

    — commented 10 months ago by Alexander Bauer

  • Hi Gerd

    You seem to have forgotten;

    • 21 hours ago - I told you about the setList, when you were using setString
    • 20 hours ago - I told you to swap setList to setObject as you had changed your needs in your latest code update
    • 19 hours ago - I told you that you had an issue with your array from PHP having no [ ] elements
    • 18 hours ago - I told you to look at the PHP output.

    Whilst I am glad you have now realised the problem, I am left wondering why my efforts were left unrewarded, especially as the one you marked did not provide any of that help or point out the correct method to store your data once you had fixed the bug I pointed out 6 hours earlier.

    — commented 10 months ago by Malcolm Hollingsworth

  • just a missclick :D ofc i used your comments

    — commented 10 months ago by Gerd Koetje

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!