0

I have the following string in my jquery function:

{"rows":[{"id_sala":"27","horas":"4","Fecha":"2014-05-05"},{"id_sala":"27","horas":"5","Fecha":"2015-04-30"}]}

and I need to convert this into an array for use it in google charts, i have tried to program my own function but I had a really bad headhache.

Any help will be appreciated.

4
  • 2
    Use the function JSON.parse() and then you'll need to iterate over it with loops. Commented May 5, 2015 at 8:17
  • $.parseJSON( jsonString ) if you have the JSON string in jQuery JSON.parse( jsonString ) if you have the JSON string in a browser Commented May 5, 2015 at 8:19
  • @evolutionxbox @detheridge02 that was my first option, but when I pass the resulting arr to the google method it sais Uncaught Error: Argument given to addRows must be either a number or an array Commented May 5, 2015 at 8:23
  • Could you add your google charts init please ? Commented May 5, 2015 at 8:25

1 Answer 1

2

Parsing of the jsonString gives you a json object like this. Object {rows: Array[2]}. Since you need an array of rows to be passed to the addRows method, you have to code as shown below.

String jsonString ='{"rows":[{"id_sala":"27","horas":"4","Fecha":"2014-05-05"},{"id_sala":"27","horas":"5","Fecha":"2015-04-30"}]}';
var rows=  JSON.parse(jsonString).rows; //Returns an array of rows
//Pass rows to the addRows method.
Sign up to request clarification or add additional context in comments.

1 Comment

Dunno why but this worked... thx man u rly saved me from a hard job

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.