Dan
Dan

Reputation: 43

parse json-like structure/string in javascript

I have this variable:

var txt = { 
    'e3fe40': { 
        'name' : 'Menu2',
        'data': {
            'prompt_say' : 'Thank you for calling ',
            'keys[]' : ['1','2'],
            'choices[]' : [
                'start/e3fe40/d2d27',
                'start/e3fe40/77c197'
            ]...

While I can parse with no problems things such as txt[position].data.... , I cannot seem to be able to parse these two:

txt[position].data.keys and txt[position].data.choices

  (last two entries from code above)

I tried with

txt[position].data.keys[1], 
txt[position].data.keys[],
txt[position].data.keys

etc.

None worked. I always get "undefined" value.

Any ideas?

Upvotes: 0

Views: 94

Answers (1)

Brad
Brad

Reputation: 163272

txt[position].data['keys[]']

The key keys[] is the name of the key. You can't arbitrarily insert JSON-like strings in as a key name and expect it to parse. This obviously assumes that the value of position is e3fe40 or whatever it needs to be.

Upvotes: 3

Related Questions