0

I'm trying to retrieve data from a source in JSON. I am able to retrieve some of the data such as "episode_name" & "overview", however I'm having issues with some data such as "writers" & "directer".

This is the code i'm using along with my example

   var jsontext = '{"first_aired":"2004-06-06","episode_name":"Let Em Eat Cake","overview":"Blahh Blahh","writers":[{"name":"Jim Vallely"},{"name":"Mitchell Hurwitz"}],"directors":[{"name":"Paul Feig"}],"guest_stars":[{"name":"Ian Roberts"},{"name":"Judy Greer"},{"name":"Stacey Grenrock-Woods"},{"name":"Matt Walsh"},{"name":"Alessandra Toreson"}]}';
    var titles = JSON.parse(jsontext);
    document.write(titles.episode_name);

Basically the problem comes down to not being able to retrieve data on a multilevel basis. I'm not sure how to do this.

Here is my example in JS Fiddle for editing it to show me. http://jsfiddle.net/k3V9p/1/

Thank You

1

2 Answers 2

1

The writers subobject is an array, so you would access its elements and subobjects like so:

titles.writers[0].name
Sign up to request clarification or add additional context in comments.

1 Comment

And if the lists are really just arrays of names, you don't even have to make them arrays of objects. Just make them arrays of strings.
0

You can collect the writer´s names with a loop

    var jsontext = '{"first_aired":"2004-06-06","episode_name":"Let Em Eat Cake","overview":"Blahh Blahh","writers":[{"name":"Jim Vallely"},{"name":"Mitchell Hurwitz"}],"directors":[{"name":"Paul Feig"}],"guest_stars":[{"name":"Ian Roberts"},{"name":"Judy Greer"},{"name":"Stacey Grenrock-Woods"},{"name":"Matt Walsh"},{"name":"Alessandra Toreson"}]}';
    var titles = JSON.parse(jsontext);
    var names = [];
    $.each(titles.writers, function(i,it){
      names.push(it.name);
    })
    alert(names);

http://jsfiddle.net/k3V9p/2/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.