oshirowanen
oshirowanen

Reputation: 15925

Extracting specific data from json using jquery

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 ?

Upvotes: 0

Views: 132

Answers (5)

Bhavesh G
Bhavesh G

Reputation: 3028

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

Upvotes: 0

Rab
Rab

Reputation: 35572

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

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150030

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...

Upvotes: 0

Cranio
Cranio

Reputation: 9847

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"
                                         },

Upvotes: 0

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

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

Upvotes: 1

Related Questions