0

I am trying to access an specific attribute in a json array. I've already looked here and it helped me for the general understanding but for my specific use case it does not work.

The json looks like this:

{
    "uuid": "3a8ed45f-28e7-4263-8437-d926c6a194f4",
    "number": "3310010",
    "shortname": "FUESTRUP",
    "longname": "FUESTRUP",
    "km": 102.177,
    "agency": "WSA RHEINE",
    "longitude": 7.680240800859249,
    "latitude": 52.04026675873374,
    "water": {
        "shortname": "EMS",
        "longname": "EMS"
    },
    "timeseries": [{
        "shortname": "W",
        "longname": "WASSERSTAND ROHDATEN",
        "unit": "cm",
        "equidistance": 15,
        "gaugeZero": {
            "unit": "m. ü. NN",
            "value": 35.69,
            "validFrom": "1952-02-01"
        }
    }]
}

Now I want to access the value-Property within the gaugeZero-Collection within the timeseries-array. I've already tried to do this by using:

data.timeseries[0].longname.gaugeZero.value

but this does not work (it's undefinied).

Thanks for your help!

1
  • 1
    JSON is just a string are you converting your JSON to a javascript object first? Commented May 19, 2014 at 22:26

4 Answers 4

4

gaugeZero is not a property of longname. You have to access it like this...

data.timeseries[0].gauzeZero.value

Assuming that data is the JavaScript object created from the JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I've made a typo - of course I know that gaugeZero is not a property of longname. Anyway...Thanks a lot :) Unfortunately now I have a different problem. I am working with Leaflet and the JsonLayer Plugin, but by accessing value with your mentioned way, I get a error (the json file is coming from an url). I've created a jsfiddle here ( check Line 33/35, which make the trick) to show you the error. Maybe you have an idea?
0

Use data.timeseries[0].gaugeZero.value instead of

data.timeseries[0].longname.gaugeZero.value

.

Comments

0
data.timeseries[0].longname.gaugeZero.value

Won't ever work, as longname doesn't have a gaugeZero property. Just the same as trying data.color or data.timeseries[0].animals as examples.

You need to:

  1. Get your JSON to an object.
  2. Then having an object data:

    data.timeseries[0].gaugeZero.value

You can test it at your browser console or at Node repl and see the magic work.

Comments

0

gaugeZero is not a property of longname

data['timeseries']["longname"]["gaugeZero"]["value"];

http://jsfiddle.net/YP68N/

1 Comment

if there an alternative you can suggest?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.