Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 410145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:51:35+00:00 2026-05-12T17:51:35+00:00

I am playing around with parsing JSON in jQuery and I am having trouble.

  • 0

I am playing around with parsing JSON in jQuery and I am having trouble. I want to check the value of ‘time’ in the JSON object. Here is my trial function:

$.ajax({
     url: 'do_chat.php5',
     type: 'post',
     data: ({'message':'','poster':poster,'logged_in':logged_in}),
     dataType: 'json',
     success: function(data) {
       $.each(data, function(interval, message) {
       if(message['time']) {
     $('#chatWindow').append('<p>hi</p>');
       }
 });
     }
     });

The string being returned from my server looks like:

{“poster”:””,”message”:”No messages!”,”time”:1256084677}

I am not sure of the syntax of $.each. I am using PHP to encode the JSON string, which started life as a php assoc array. I’m sure my mistakes are plenty and any help is greatly appreciated!

Edit: follow up question – how do I make PHP generate a JSON array? At the moment I use this to generate the single object:

$messages = mysqli_fetch_assoc($new_res);
 $msg = $messages["content"];
 $who = $messages["poster"];
 $time = $messages["time_added"];
$message = array(
  'poster' => $who,
  'message' => $msg,
  'time' => $time
);

echo json_encode($message);

But if I were to get more than one row from my database query, how do I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:51:35+00:00Added an answer on May 12, 2026 at 5:51 pm

    The problem with your code is that you are returning a single JSON Object {"poster":"","message":"No messages!","time":1256084677}, but you are then iterating through it with $.each.

    $.each expects an array, and since you are not returning an array, the $.each is looping through the elements of your JSON object instead.

    To fix your code, you need to either make sure your server is returning an array, like:
    [{"poster":"","message":"No messages!","time":1256084677}]

    or

    Remove the $.each, so that you have:

    $.ajax({
        url: 'do_chat.php5',
        type: 'post',
        data: ({'message':'','poster':poster,'logged_in':logged_in}),
        dataType: 'json',
        success: function(data) {
            if(data['time']) {
                $('#chatWindow').append('<p>hi</p>');
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just playing around with memcache for the first time; here's my code: $memcache =
i'm playing around with flickr API lately and having difficulties in parsing their response
I'm playing around with strncpy in C and am having some trouble. The code
I'm playing around with FLOT with Ruby and I'm having a hard time passing
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Im playing around with TryParse() But lets say the parsing fails, then returns false,
I have been playing around with Scala parser combinators for some time now, and
I'm playing around with a bash script, parsing the output of a cURL POST,
Working on a [rails] project, I've started playing around with RSS parsing. I notice
i'm new to android development and i'm playing around with json data. I managed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.