0

I am converting a Ruby array to JSON, saving to MySQL and then loading into KnockoutJS. The problem is that the array stays a JSON string and I am unable to iterate over it.

tags = `/usr/bin/svn ls #{svn_repo_url}`.split("/\n")

puts tags.inspect
["1.0.0", "1.0.1", "1.0.10", "1.0.11", "1.0.12", "1.0.13", "1.0.14", "1.0.15", "1.0.16", "1.0.2", "1.0.3", "1.0.4", "1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9"]

puts tags.to_json
["1.0.0","1.0.1","1.0.10","1.0.11","1.0.12","1.0.13","1.0.14","1.0.15","1.0.16","1.0.2","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8","1.0.9"]

This gets saved to MySQL and then gets loaded into KnockoutJS, but it stays as a string, so I am unable to iterate over it in a foreach loop.

I tried to do ko.mapping.toJS(myString) and ko.toJSON(myString) but so far having no luck and unable to convert to an an actual array or object I can iterate over

What am i doing wrong here?

Thank you

UPDATE: Solved with eval(myString)

solefald
  • 1,739
  • 1
  • 15
  • 29
  • If you can, don't use eval, use JSON.parse as sporkydorky said : http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – pax162 Nov 07 '13 at 05:27
  • @pax162: i've tried that, but it throws `Message: Unexpected end of input` – solefald Nov 07 '13 at 05:45
  • Are you sure the string is valid JSON? Are you wrapping it in proper quotes? – pax162 Nov 07 '13 at 05:49
  • @pax162: I *think* so. If it wasn't valid `eval()` would not work, right? Plus its rather a simple flat array of numbers. Not much room for error there.... – solefald Nov 07 '13 at 06:00
  • how does the value look in the html/js file? At some point you must have some var x = "your_array". How does it look there? – pax162 Nov 07 '13 at 06:03
  • @pax162: like this: `["1.0.0","1.0.1","1.0.10","1.0.11","1.0.12","1.0.13","1.0.14"]`. I also tried creating the object in Ruby like this: `array.map { |tag| {:tag => tag} }.to_json`. which resulted in `[{"tag":"trunk"},{"tag":"1.0.0"},{"tag":"1.0.1"},{"tag":"1.0.10"},{"tag":"1.0.11"}]`. This one also returns an error if i do `JSON.parse()` – solefald Nov 07 '13 at 06:13
  • Check out this: http://jsfiddle.net/9dZmb/1/ . Do you notice anything different in your code? – pax162 Nov 07 '13 at 06:25
  • @pax162: see some strange dot in the last number, but thats the only difference. This is the actual `consle.log(myString)` produces `[{"tag":"trunk"},{"tag":"0.0.0"},{"tag":"0.0.10"},{"tag":"0.0.11"},{"tag":"0.0.12"},{"tag":"0.0.13"},{"tag":"0.0.3"},{"tag":"0.0.4"},{"tag":"0.0.5"},{"tag":"0.0.6"},{"tag":"0.0.7"},{"tag":"0.0.8"},{"tag":"0.0.9"}]`, but when i do `console.log(JSON.parse(mystring))` I get `Uncaught SyntaxError: Unexpected end of input ` – solefald Nov 07 '13 at 07:09
  • Why don't you paste your code in an updated fiddle. Paste also some of the sorrounding code. Are you sure the error is triggered by the JSON.parse? – pax162 Nov 07 '13 at 07:14
  • @pax162: ha! this solved it! http://stackoverflow.com/questions/9158665/json-parse-fails-in-google-chrome – solefald Nov 07 '13 at 07:14
  • OK, don't forget to accept sporkydorky's answer if it worked. – pax162 Nov 07 '13 at 07:16

1 Answers1

1

Before iterating over the array, you can use JSON.parse(array) to convert it into a JavaScript array.

sp0rkyd0rky
  • 290
  • 1
  • 8