1

Here is my JSON returned from a PHP file (just as a side note, I can use EITHER one):

// JSON Option #1:

[{"field":"title_of_production_row", "required":"1"}{"field":"where_released_row", "required":"1"}{"field":"release_date_row", "required":"1"}{"field":"running_time_row", "required":"1"}{"field":"accepting_award_row", "required":"1"}{"field":"contact_name_row", "required":"1"}{"field":"promocode_row", "required":"0"}{"field":"payment_method_row", "required":"1"}{"field":"tbl_submit", "required":"0"}{"field":"production_company_row", "required":"1"}]

// JSON Option #2: {"title_of_production_row":"1","where_released_row":"1","release_date_row":"1","running_time_row":"1","accepting_award_row":"1","contact_name_row":"1","promocode_row":"0","payment_method_row":"1","tbl_submit":"0","production_company_row":"1"}

I want to loop through each field and required, and alert them. I've tried things like:

    $.ajax({
        url: './ajax/get_cat_info.php?cid=' + cid,
        dataType: "jason",
        async: false,
        success: function(html) {
            alert(html);

         $.each(html, function(key, val) {
            alert('key: ' + key + ' - val: ' + val);
         });

        }
    });

But this alerts individual characters. Any thoughts?

1
  • Just so you know, with the correct answers given below, your alerts are going to show a numerical key and [object Object] for value. The first param passed to .each() is the current index in the each "loop", the second is the current element from the eached collection. Since the items in your array are objects, the alert casts them to string and you won't see anything of use. Commented Aug 14, 2011 at 0:32

3 Answers 3

2
$.ajax({
    url: './ajax/get_cat_info.php?cid=' + cid,
    dataType: "json", // Need a correct dataType
    async: false,
    success: function(html) {
        alert(html);

     $.each(html, function(key, val) {
        alert('key: ' + key + ' - val: ' + val);
     });
    }
});

JSON Option 1 was invalid: Try this:

[
    {
        "field": "title_of_production_row",
        "required": "1"
    },
    {
        "field": "where_released_row",
        "required": "1"
    },
    {
        "field": "release_date_row",
        "required": "1"
    },
    {
        "field": "running_time_row",
        "required": "1"
    },
    {
        "field": "accepting_award_row",
        "required": "1"
    },
    {
        "field": "contact_name_row",
        "required": "1"
    },
    {
        "field": "promocode_row",
        "required": "0"
    },
    {
        "field": "payment_method_row",
        "required": "1"
    },
    {
        "field": "tbl_submit",
        "required": "0"
    },
    {
        "field": "production_company_row",
        "required": "1"
    }
]

For future reference, a great resource is JSONLint.com which allows you to validate your JSON on the fly.

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

Comments

2

It looks like you've got two issues:

  1. The JSON in your example is invalid. It should be [{…}, {…}, …] (noete the commas).
  2. Your jQuery call has "jason", not "json"

Comments

1

You made a typo with the datatype

`dataType: "jason"`,

should be

`dataType: "json",`

1 Comment

Heh. Wow. Thanks! I've been at this for too long today!

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.