Jun 15, 2012, 10:27 AM
53 Posts

JSON?

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.3
  • Role: Developer
  • Tags: Repeat,JSON
  • Replies: 2
I've got a repeat in which I'm trying to create the data as a simple bit of JSON. I'm not a javascript expert so may be creating the JSON incorrectly, or simply not referring to the data name properly, but I'm not getting anything out in my repeat control
 
In the repeat I have something like this (simplified):
 
var out = new Array();
out.push('{"string1": "something", "string2":"something"}');
out.push('{"string1": "something", "string2":"something"}');
return out
 
I've checked out for isJson and it returns true, and tried return toJson(out)
 
If my repeat variable is rowData, how do I get the value of string1 on each line of the repeat? I thought I'd just use rowData.string1, which is what I've seen when searching for this sort of idea, but that's obviously wrong
 
Cheers for any help
Jun 18, 2012, 7:32 AM
261 Posts
Re: JSON?
Dan,
 
You should add the individual objects to the array as objects, not as strings. Sample:
 
var out = [];   //shortcut for new Array();
out.push( {"string1" : "something", "string2" : "something" } );
out.push( {"string1" : "something", "string2" : "something" } );
return out;
 
If your variable is called rowData you can refer to it like you already tried: rowData.string1
 
Mark
 
Jun 18, 2012, 9:53 AM
53 Posts
Re: JSON?
Thanks Mark, yes that's working great.
 
Although I do have a problem with building the object. The actual code is a bit more complex and I build the object's string through various lines of  SSJS (in a loop):
 
 thisout+="'dayString':'" + I18n.toString(ndt.toJavaDate(),"dd") + "', ";
 thisout+="'timeString':'" + ve.getColumnValues()[5] + "', ";
 thisout+="'titleString':'" +ve.getColumnValues()[2] + "', ";
 thisout+="'iconString':'" + ve.getColumnValues()[6] + "'";
 
Assigning this string as an object to the array is problematic though as well, can you give me a clue as to how to do this.
 
I've actually got round it by building an object:
thisObj.dayString = I18n.toString(ndt.toJavaDate(),"dd");
 etc.
 
Maybe this is the best way to do it anyway, but would appreciate your thoughts on that