Saurabh Verma
Saurabh Verma

Reputation: 6728

JavaScript Json Array Parsing using Backbone

I have the following sample json:

{
    "camp": [
        {
            "name": "Name",
            "data": [
                {
                    "date": "04/08/2014",
                    "value": 1000
                },
                {
                    "date": "05/08/2014",
                    "value": 1110
                }
            ]
        }
    ]
}

Here, I'm able to do: model.get("camp")[0], but when I try: model.get("camp")[0].get("data"), I get the following error:

undefined is not a function 

Here model is the standard backbone model which extends Backbone.Model I'm confused what I'm doing wrong !!

Upvotes: 0

Views: 64

Answers (3)

Bart van der Drift
Bart van der Drift

Reputation: 1336

You only need to call the model.get() function once. After that, you can treat the returned object just like any other javascript-object. For example, you could do this to get one of the values deep inside the object:

model.get("camp")[0].data[0].value

To achieve what you are trying to get, do this:

model.get("camp")[0].data

Upvotes: 2

reignsly
reignsly

Reputation: 502

This is how you get your value:

obj.camp[0].data[0]

Upvotes: -1

ji_bay_
ji_bay_

Reputation: 221

If you want to acces a property of your json array, you should simply do like this:

var test = json.camp.name

Upvotes: 1

Related Questions