最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Navigating through JSON with JavaScript - Stack Overflow

programmeradmin5浏览0评论

OK, I have been searching through this site to find anything similar or enlightening, but I am completely stuck. I am getting some valid JSON and need to parse through it to extract prices. I'm not getting very far.

Here's the JSON:

{
  "result": "success",
  "prices": {
    "vdc": {
      "monthly": "1.00"
    },
    "network": {
      "private": {
        "monthly": "2.00"
      },
      "public": {
        "\/22 (1,111 IP Addresses)": {
          "monthly": "3.00"
        },
        "\/21 (2,222 IP Addresses)": {
          "monthly": "4.00"
        },
        "\/20 (3,333 IP Addresses)": {
          "monthly": "5.00"
        },
        "\/19 (5,555 IP Addresses)": {
          "monthly": "6.00"
        },
        "\/18 (6,666 IP Addresses)": {
          "monthly": "7.00"
        },
        "\/17 (7,777 IP Addresses)": {
          "monthly": "8.00"
        },
        "\/16 (8,888 IP Addresses)": {
          "monthly": "9.00"
        },
        "\/25 (111 IP Addresses)": {
          "monthly": "10.00"
        },
        "\/26 (55 IP Addresses)": {
          "monthly": "11.00"
        },
        "\/27 (22 IP Addresses)": {
          "monthly": "12.00"
        },
        "\/28 (11 IP Addresses)": {
          "monthly": "13.00"
        },
        "\/29 (5 IP Addresses)": {
          "monthly": "14.00"
        },
        "\/23 (900 IP Addresses)": {
          "monthly": "15.00"
        },
        "\/24 (333 IP Addresses)": {
          "monthly": "16.00"
        }
      }
    },
    "blocks": {
      "22": {
        "monthly": "17.00"
      },
      "21": {
        "monthly": "18.00"
      },
      "20": {
        "monthly": "19.00"
      },
      "19": {
        "monthly": "20.00"
      },
      "18": {
        "monthly": "21.00"
      },
      "17": {
        "monthly": "22.00"
      },
      "16": {
        "monthly": "23.00"
      },
      "25": {
        "monthly": "24.00"
      },
      "26": {
        "monthly": "25.00"
      },
      "27": {
        "monthly": "28.00"
      },
      "28": {
        "monthly": "29.00"
      },
      "29": {
        "monthly": "30.00"
      },
      "23": {
        "monthly": "24.00"
      },
      "24": {
        "monthly": "25.00"
      }
    },
    "server": {
      "cpu": {
        "monthly": "26.00"
      },
      "ram": {
        "monthly": "27.00"
      }
    },
    "volume": {
      "gb": {
        "monthly": "28.00"
      }
    },
    "snapshot": {
      "gb": {
        "monthly": "29.00"
      }
    }
  }
}

Tested and validated at jsonlint [dot] com.

After much trying, testing, trying, banging head against my keyboard, trying...this is what I currently have, but it's not producing the desired results (I'll tell you what those are, right after the code snippet).

function gp(x){ 
    for(var i in x){ 
        console.log('700: ',  x[i] );

        if(x[i] != 'success'){
            console.log(733);
            console.log(x[i]);

            for(var j in x[i]){
                console.log(736);
                console.log(x[i][j]);
            }
        }
     }
}

In the console, I see something like this:

What I really would like to see (or find or parse to) is, for example, the monthly price for "gb" from the "volume" element (or is it an item?).

Ideally, I would like to find "volume", "ram" and "cpu" – verify that it's the volume, ram and cpu – then get the monthly price. I did try a few things with JSON parsing, but obviously, I haven't mastered that quite yet either.

Any help would be greatly appreciated.

OK, I have been searching through this site to find anything similar or enlightening, but I am completely stuck. I am getting some valid JSON and need to parse through it to extract prices. I'm not getting very far.

Here's the JSON:

{
  "result": "success",
  "prices": {
    "vdc": {
      "monthly": "1.00"
    },
    "network": {
      "private": {
        "monthly": "2.00"
      },
      "public": {
        "\/22 (1,111 IP Addresses)": {
          "monthly": "3.00"
        },
        "\/21 (2,222 IP Addresses)": {
          "monthly": "4.00"
        },
        "\/20 (3,333 IP Addresses)": {
          "monthly": "5.00"
        },
        "\/19 (5,555 IP Addresses)": {
          "monthly": "6.00"
        },
        "\/18 (6,666 IP Addresses)": {
          "monthly": "7.00"
        },
        "\/17 (7,777 IP Addresses)": {
          "monthly": "8.00"
        },
        "\/16 (8,888 IP Addresses)": {
          "monthly": "9.00"
        },
        "\/25 (111 IP Addresses)": {
          "monthly": "10.00"
        },
        "\/26 (55 IP Addresses)": {
          "monthly": "11.00"
        },
        "\/27 (22 IP Addresses)": {
          "monthly": "12.00"
        },
        "\/28 (11 IP Addresses)": {
          "monthly": "13.00"
        },
        "\/29 (5 IP Addresses)": {
          "monthly": "14.00"
        },
        "\/23 (900 IP Addresses)": {
          "monthly": "15.00"
        },
        "\/24 (333 IP Addresses)": {
          "monthly": "16.00"
        }
      }
    },
    "blocks": {
      "22": {
        "monthly": "17.00"
      },
      "21": {
        "monthly": "18.00"
      },
      "20": {
        "monthly": "19.00"
      },
      "19": {
        "monthly": "20.00"
      },
      "18": {
        "monthly": "21.00"
      },
      "17": {
        "monthly": "22.00"
      },
      "16": {
        "monthly": "23.00"
      },
      "25": {
        "monthly": "24.00"
      },
      "26": {
        "monthly": "25.00"
      },
      "27": {
        "monthly": "28.00"
      },
      "28": {
        "monthly": "29.00"
      },
      "29": {
        "monthly": "30.00"
      },
      "23": {
        "monthly": "24.00"
      },
      "24": {
        "monthly": "25.00"
      }
    },
    "server": {
      "cpu": {
        "monthly": "26.00"
      },
      "ram": {
        "monthly": "27.00"
      }
    },
    "volume": {
      "gb": {
        "monthly": "28.00"
      }
    },
    "snapshot": {
      "gb": {
        "monthly": "29.00"
      }
    }
  }
}

Tested and validated at jsonlint [dot] com.

After much trying, testing, trying, banging head against my keyboard, trying...this is what I currently have, but it's not producing the desired results (I'll tell you what those are, right after the code snippet).

function gp(x){ 
    for(var i in x){ 
        console.log('700: ',  x[i] );

        if(x[i] != 'success'){
            console.log(733);
            console.log(x[i]);

            for(var j in x[i]){
                console.log(736);
                console.log(x[i][j]);
            }
        }
     }
}

In the console, I see something like this:

What I really would like to see (or find or parse to) is, for example, the monthly price for "gb" from the "volume" element (or is it an item?).

Ideally, I would like to find "volume", "ram" and "cpu" – verify that it's the volume, ram and cpu – then get the monthly price. I did try a few things with JSON parsing, but obviously, I haven't mastered that quite yet either.

Any help would be greatly appreciated.

Share Improve this question edited Jan 4, 2013 at 3:36 xivo 2,0543 gold badges22 silver badges37 bronze badges asked Jan 4, 2013 at 3:22 AceAce 6644 gold badges10 silver badges20 bronze badges 9
  • 3 I think you're talking about navigating an object, not parsing. The word "parse" is about interpreting a string to recognize a syntax. It looks like you've already had your JSON string parsed into an object. – Pointy Commented Jan 4, 2013 at 3:25
  • So I should look for help with navigating an object then, right. OK. I really appreciate your feedback. Thanks. – Ace Commented Jan 4, 2013 at 3:29
  • how did you get your json in the first place. is it by using ajax or post or what exactly. knowing that might help to post a suitable answer for you – mamdouh alramadan Commented Jan 4, 2013 at 3:30
  • volume can be found using x.volume, ram can be found using x.server.ram, and cpu can be found using x.server.cpu. Does that answer your question? – user1726343 Commented Jan 4, 2013 at 3:30
  • What is the o/p format you are looking at? – Arun P Johny Commented Jan 4, 2013 at 3:34
 |  Show 4 more comments

2 Answers 2

Reset to default 11

If you want to find the objects "volume", "ram" and "cpu" that's simple:

var volume = x.prices.volume;
var ram = x.prices.server.ram;
var cpu = x.prices.server.cpu;

or you can simply use them directly:

console.log(x.prices.volume);

If you want to find the monthly prices then:

var prices = x.prices;
console.log('volume, monthly=', prices.volume.gb.monthly);
console.log('cpu, monthly=', prices.server.cpu.monthly);
console.log('ram, monthly=', prices.server.ram.monthly);

Javascript objects are really simple, there are only 2 syntax for accessing them:

// If the key you're accessing is a constant (hardcoded):
object.key = value;

// If the key you're accessing is stored in another variable:
var k = "key";
object[k] = value;

// Alternatively:
object["key"] = value;

I'm just a bit confused, but heres an example. Save your json to an variable and just go down the tree with node names (unless don't have any).

This should get the volume monthly

 json.prices.volume.gb.monthly 

http://jsfiddle.net/PKUBA/

发布评论

评论列表(0)

  1. 暂无评论