开发者

JSON Jquery parse

开发者 https://www.devze.com 2023-02-03 12:45 出处:网络
开发者_运维知识库I\'ve been stuck trying to parse this out. I\'ve tried google and it doesn\'t seem to work...
开发者_运维知识库

I've been stuck trying to parse this out. I've tried google and it doesn't seem to work...

Here's the JSON:

{
"1": [
    {
        "SUBCATEGORY_ID": "1",
        "CATEGORY_ID": "1",
        "NAME": "Sonic",
        "SORTS": "1" 
    } 
],
"2": [
    {
        "SUBCATEGORY_ID": "2",
        "CATEGORY_ID": "2",
        "NAME": "Captain Planet",
        "SORTS": "1" 
    },
    {
        "SUBCATEGORY_ID": "3",
        "CATEGORY_ID": "2",
        "NAME": "Rocco Mordern life",
        "SORTS": "2" 
    },
    {
        "SUBCATEGORY_ID": "4",
        "CATEGORY_ID": "2",
        "NAME": "Sponge BOB",
        "SORTS": "3" 
    } 
]

}

This is my jquery code:

jQuery(document).ready(function(){
    $.ajax({
        dataType: 'json',
        url: 'subcategoriesAjax.php',
        success: function(data){
                //alert(data.1[0]);
        }
    }); 
}); // $(document).ready(function(){

It doesn't seem to work.

I've also tried data.1.0 & data.1[0].SORTS.

Thank you!


data["1"][0]

should do the trick


You can't use the dot notation to access properties when they are numbers. Use the array-like notation instead:

data['1'][0]

Even better would be to use something other than a number as the key, or to use a normal array.


try:

...

success: function(data){ alert(data.d); }

...


alert(data["1"][0]["SUBCATEGORY_ID"]);

Result of this alert is 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消