0

From the following json snippet:

cbfunc22({
  query: {
    count: 2,
      created: "2012-06-18T11:18:15Z",
      lang: "en-US",
      results: {
        tbody: [{
            tr: [{
                class: "day",
                td: [{
                    class: "dayname",
                    p: "Monday"
                  },
                  {
                    class: "weather",
                    p: "Sunny Intervals"
                  },

I can extract Monday using jQuery as follows:

data.query.results.tbody[0].tr[0].td[0].p

How do I extract Sunny Intervals ?

2
  • data.query.results.tbody[0].tr[0].td[1].p jsfiddle.net/joycse06/RYdvs Commented Jun 18, 2012 at 11:25
  • 1
    That's not JSON. (Given the function syntax at the beginning is it perhaps the return of a jsonp call?). You need to be careful about object literals that have unquoted property names if you're going to include names ("class") that are JS reserved words. Commented Jun 18, 2012 at 11:34

5 Answers 5

2
data.query.results.tbody[0].tr[0].td[1].p
Sign up to request clarification or add additional context in comments.

Comments

1
$('tbody > tr > td > p:nth-child(2)')

Comments

0

Just use:

data.query.results.tbody[0].tr[0].td[1].p 

If you indent your code correctly the solution is more apparent:

cbfunc22(
{
    query: {
        count: 2,
        created: "2012-06-18T11:18:15Z",
        lang: "en-US",
        results: {
            tbody: [
                    {
                        tr: [
                                {
                                    class: "day",
                                    td: [
                                         {
                                               class: "dayname",
                                           p: "Monday"
                                         },
                                         {
                                           class: "weather",
                                           p: "Sunny Intervals"
                                         },

Comments

0

td is an array, so index 0 gives the first element, index 1 gives the second:

data.query.results.tbody[0].tr[0].td[1].p

and so forth...

Comments

0

td[0] is for Monday so td[1] is for Sunny Intervals and so on

so Sunny Intervals can be extracted by,

data.query.results.tbody[0].tr[0].td[1].p

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.