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 6927535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:04:05+00:00 2026-05-27T11:04:05+00:00

My Json objects are inside a external file named abc.json in my js file

  • 0

My Json objects are inside a external file named abc.json in my js file I am calling it. I have a html markup like this

<li class="" data-flight-name="kingfisher" data-flight-id="E6-184"></li>
<li class="" data-flight-name="indigo" data-flight-id="E6-185"></li>

My requirement is when ever i click on that LI pass this data-flight-name and data-flight-id while fetching the information through JSON but while fetching I am not getting proper data.

My Json File structure is

{
  "flightInfo" :{
    "indigo": [
          {"E-184" : {"flightName": "Indigo", "duration": "1h:0m", "baseFare": 1753, "travelTime": "11:13 - 12:13"}},
          {"E-185" : {"flightName": "Indigo", "duration": "1h:0m", "baseFare": 1753, "travelTime": "11:13 - 12:13"}},
          {"E-186" : {"flightName": "Indigo", "duration": "2h:30m", "baseFare": 8000, "travelTime": "11:13 - 16:13"}},
          {"E-187" : {"flightName": "Indigo", "duration": "6h:30m", "baseFare": 6000, "travelTime": "11:13 - 16:13"}},
          {"E-189" : {"flightName": "Indigo", "duration": "5h:0m", "baseFare": 9907, "travelTime": "11:13 - 16:13"}}
      ],

    "kingfisher": [
          {"E-184" : {"flightName": "KF RED", "duration": "1h:0m", "baseFare": 2819, "travelTime": "10:13 - 11:13"}},
          {"E-184" : {"flightName": "KF RED", "duration": "1h:0m", "baseFare": 2819, "travelTime": "11:13 - 16:13"}},
          {"E-184" : {"flightName": "KF RED", "duration": "2h:30m", "baseFare": 18000, "travelTime": "11:13 - 16:13"}},
          {"E-184" : {"flightName": "KF RED", "duration": "6h:30m", "baseFare": 16000, "travelTime": "11:13 - 16:13"}}
      ]
  }
}

Used jquery for fetching information is

$("#list li").delegate("", "click", function(e){
        var dataflightname = $(this).attr('data-flight-name'),
            dataflightid = $(this).attr('data-flight-id');
//            console.log(dataflightname);
//            console.log(dataflightid);
        if($(this).siblings().hasClass('selected')){
          $(this).siblings().removeClass('selected');
          $(this).addClass('selected');
          // Featcing Flights Information
          $.getJSON("javascript/abc.json",function(data){

            $(data.flightInfo[dataflightname][0]).each(function(key, items) {
              $(data.flightInfo[dataflightname][key]).each(function(key, items) {
              //Reading name tag value
              //here i am not getting proper value
              console.log(data.flightInfo[dataflightname][0][items]);
              });
            });
          });
        }
      });

Please help to understand why I am not able to get the all values.

  • 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-27T11:04:05+00:00Added an answer on May 27, 2026 at 11:04 am

    You have the syntax for .delegate() a bit mixed up, it goes: $(<root element>).delegate(<selector>, <event>, <callback>). When you try to access your JSON object you append a [0] onto the end which is unnecessary (data.flightInfo[dataflightname][0] should be data.flightInfo[dataflightname]):

    $("#list").delegate("li", "click", function(e){
        var $this          = $(this),
            dataflightname = $this.attr('data-flight-name'),
            dataflightid   = $this.attr('data-flight-id');
    
        if($this.siblings().hasClass('selected')){
          $this.siblings().removeClass('selected');
          $this.addClass('selected');
    
          // Featcing Flights Information
          $.getJSON("javascript/abc.json",function(data){
            for (var i = 0, len = data.flightInfo[dataflightname].length; i < len; i++) {
                console.log(data.flightInfo[dataflightname][i]);
                //output will be (for the first index): {"E-184" : {"flightName": "Indigo", "duration": "1h:0m", "baseFare": 1753, "travelTime": "11:13 - 12:13"}}
            }
          });
        }
      });
    

    Here is a demo: http://jsfiddle.net/HscZX/

    Notice I cached the $(this) selector in the $this variable since it was being used so many times which should improve performance. Also I changed out the $.each() loop for a for (var i = 0, len = data.length; i < len; i++) {} beacuse the later performs much faster. Check-out this jsperf to see the difference in performance: http://jsperf.com/for-in-tests/4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large list of objects inside JSON like this: var data =
I have JSON object inside variable like this: var chessPieces = { p-w-1 :
I have an array of json objects like so: [{a:b},{c:d},{e:f}] What is the best
Purpose of this web service is to server html/javascript/css/binary data inside simple XML document
I have this ConstantData class which holds my JSON indexes, I need to pass
I have this JSON data: var people = [ { name : John, age
i have some json objects and some of them have some other objects inside
I have two JSON objects with the same structure and I want to concat
I need a tool which generates random JSON objects. I want to use this
I use an asp (classic) utility file to create json objects from an SQL

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.