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

  • SEARCH
  • Home
  • 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 8704191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:02:22+00:00 2026-06-13T03:02:22+00:00

I am trying to print simple JSON data to understand and learn about JSON.

  • 0

I am trying to print simple JSON data to understand and learn about JSON. I can’t quite figure out what I am doing wrong here.

I am following this tutorial here
http://www.youtube.com/watch?v=rncW-74VL7U

This is my JavaScript:

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$.getJSON("json.php", function(data)
{
    //json.php get the JSON data from here
    //function(data) callback function
    var itemslist = data.items[0];

    document.write(itemslist.key);
});
</script>

Here is the PHP

<?php

// Set up the PHP to return the data in JSON format
header("content-type: application/json; charset=UTF-8" );

// This is a array of objects [key:"value", key2 : {k:"val", k2:"val2"}]
$myobj = '{items : [key:"value", key2 : {k:"val", k2:"val2"}]}';

$count = 2;

// Encode the above variables in JSON
echo json_encode(array("items" => $myobj, "count" => $count));

Help is much appreciated…

  • 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-06-13T03:02:24+00:00Added an answer on June 13, 2026 at 3:02 am

    First, your json had two items nodes in it. The items in the json_encode and the items in the myobj string (now array). This is why data.items[0] wasn’t working, it needed to be data.items.items[0]. That said, what I first sent you was using keys not indexes so it really needed to be data.items.items.key. All of that said, the current structure I’ve included should be better.

    Second, try using console.debug more. I just copied your code on my dev box and ran it and found out most of the json issues by seeing what the object looked like (I’ve included the current console.debug(data) output in the comments to the javascript). Here is a link to info about chromes debug: https://developers.google.com/chrome-developer-tools/docs/console

    PHP

    <?php
    
    // Set up the PHP to return the data in JSON format
    header("content-type: application/json; charset=UTF-8" );
    
    // This is a array of objects [key:"value", key2 : {k:"val", k2:"val2"}]
    $myobj= array(
              "value",
               array("k"=>"val","k2"=>"val2")
              );
    
    $count = 2;
    
    // Encode the above variables in JSON
    echo json_encode(array("items" => $myobj, "count" => $count));
    ?>
    

    Javascript

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script>
    $.getJSON("json.php", function(data){
        console.debug(data);
        var itemslist = data.items[0];
                console.debug(itemslist);
        document.write(itemslist);
    });
    
    /*
    Object
        count: 2
        items: Object
            0: "value"
            1: Object
                k: "val"
                k2: "val2"
    */
    
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a very simple program, I want to print out the
I'm trying to figure out how to POST JSON from Android by using HTTPClient.
Okay, what have I done wrong here? I'm trying to .POST data from a
I am trying to output some data from Perl to JSON. I can do
I have a simple question concerning Javascript. I am trying to print in a
I am trying to accomplish something very simple, and I'm hoping someone can point
Im trying print Excel file data on a page. To do it i used
Problem, simple and annoying. Im just trying to print a list of names, collected
I am trying to send JSON data from a form using the XMLHttpRequest object.
I have a simple JSON object graph I'm trying to render using a Mustache

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.