0

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!

1
  • Have you tried data["1"][0].SORTS? :) Commented Jan 11, 2011 at 19:55

4 Answers 4

4
data["1"][0]

should do the trick

Sign up to request clarification or add additional context in comments.

2 Comments

You don't even need the double quotes
technically true, only done here for readability, to distinguish between using the [] operators for array access and object access.
1

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.

Comments

0

try:

...

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

...

Comments

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

Result of this alert is 1.

2 Comments

It didn't work but thank you. alert(data["1"][0].SUBCATEGORY_ID); This worked though.
I tried my solution and it worked for me. I'm glad you solved your problem. Cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.