Parsing JSON


#1

Hello All,
Im receiving a JSON response in my IONIC app which looks like this,

[{“id”:“1”,“tid”:“1”,“name”:“Mr Doctor Test”,“age”:“40”,“sex”:“Male”,“exp”:“15”,“speciality”:“Gynecology”,“qualification”:“MBBS”,“title”:“Doctor”,“timings”:“10:00,10:30,11:00,11:30,12:00”}]

How do I extract the timings as an array or object (split comma seperated) and ng repeat over that ? What do I need to do in my controller ?

So basically like < li > {{x.time}} < /li > (10:00 here)
< li > {{x.time}} < /li > (10:30 here) etc


#2

Use split () method on timings string.

var array = string.split(’,’);

This will give you an array of timings you can iterate over.


#3

How do I get the timings node as a string ?


#4

It is already a string.


#5

Let me be more precise, how do I assign it to a string variable ?
As of now I get the entire JSON data as an object. How do I extract only the ‘timings’ node ?
Only after that can I split the string right ?


#6

Assuming the above JSON has already been parsed by Angular into javascript objects, it’s an array so you’ll need to treat it as such. Let’s assume “response” contains the above, parsed by angular.

var timingsString = response[0].timings;
var timings = timingsString.split(','); //Array of the timings

Should be then able to assign it to scope and iterate it over it as usual.


#7

Thank you very much, I got it to work. @tomkuipers @andrewmcgivery


#8

Exactly, thanks for the example @andrewmcgivery