Question

What is a SIMPLE way to load an external JSON file to JavaScript (WITHOUT USING JQUERY)?...

What is a SIMPLE way to load an external JSON file to JavaScript (WITHOUT USING JQUERY)?

Example: JSON File:

{

"Orders":

[

{

"ordernumber": 83189393,

"time": "2:54:18 AM",

"customer":

{

"customerid": 66719,

"firstName": "Lucy",

"lastName": "Jones",

"city": "Dallas",

"state: "Texas"

}

}

]

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<html>

<script type="text/javascript">

var httpReq = new XMLHttpRequest();

httpReq.open('GET', 'http://localhost/HomeworkLib/json/readme.json', true);

httpReq.responseType = 'blob';

httpReq.onload = function (e) {

if (this.status == 200) {

var file = new File([this.response], 'temp');

var fileReader = new FileReader();

fileReader.addEventListener('load', function () {

//Parse JSON data

var jsonData = JSON.parse(fileReader.result);

//Print the data on HTML

document.getElementById('ono').innerText = jsonData.Orders[0].ordernumber;

document.getElementById('time').innerText = jsonData.Orders[0].time;

var customer = jsonData.Orders[0].customer;

document.getElementById('cid').innerText = customer.customerid;

document.getElementById('fname').innerText = customer.firstName;

document.getElementById('lname').innerText = customer.lastName;

document.getElementById('city').innerText = customer.city;

document.getElementById('state').innerText = customer.state;

});

fileReader.readAsText(file);

}

}

httpReq.send();

</script>

<style type="text/css">

p {

color: green;

font-weight: 600;

}

</style>

<body>

Order Number: <p id='ono'></p>

Time<p id='time'></p>

Customer Id<p id='cid'></p>

First Name<p id='fname'></p>

Last Name<p id='lname'></p>

City<p id='city'></p>

State<p id='state'></p>

</body>

</html>

<html>

<script type="text/javascript">

var httpReq = new XMLHttpRequest();

httpReq.open('GET', 'http://localhost/HomeworkLib/json/readme.json', true);

httpReq.responseType = 'blob';

httpReq.onload = function (e) {

if (this.status == 200) {

var file = new File([this.response], 'temp');

var fileReader = new FileReader();

fileReader.addEventListener('load', function () {

//Parse JSON data

var jsonData = JSON.parse(fileReader.result);

//Print the data on HTML

document.getElementById('ono').innerText = jsonData.Orders[0].ordernumber;

document.getElementById('time').innerText = jsonData.Orders[0].time;

var customer = jsonData.Orders[0].customer;

document.getElementById('cid').innerText = customer.customerid;

document.getElementById('fname').innerText = customer.firstName;

document.getElementById('lname').innerText = customer.lastName;

document.getElementById('city').innerText = customer.city;

document.getElementById('state').innerText = customer.state;

});

fileReader.readAsText(file);

}

}

httpReq.send();

</script>

<style type="text/css">

p {

color: green;

font-weight: 600;

}

</style>

<body>

Order Number: <p id='ono'></p>

Time<p id='time'></p>

Customer Id<p id='cid'></p>

First Name<p id='fname'></p>

Last Name<p id='lname'></p>

City<p id='city'></p>

State<p id='state'></p>

</body>

</html>

Add a comment
Know the answer?
Add Answer to:
What is a SIMPLE way to load an external JSON file to JavaScript (WITHOUT USING JQUERY)?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Questions are based on the Northwind database. a. A data dictionary (Excel file) that describes each...

    Questions are based on the Northwind database. a. A data dictionary (Excel file) that describes each of the tables. (Attached in question) b.    Write good, clean SQL that answers the following questions. c.     Separate your queries as I have done in the following example. End each query with a semicolon (;) and insert the word "go" between each query. Queries can span multiple lines if required. Select CustomerID from Customers; go Select Count(*) from Employees; go Select max(productID) from Products; 18. Produce...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • Summarize the following information in easily way and in simple words as your understanding without missing the keywords. ( the Question will be 25 on this information in final ) MARKETING RESEARCH FO...

    Summarize the following information in easily way and in simple words as your understanding without missing the keywords. ( the Question will be 25 on this information in final ) MARKETING RESEARCH FOR THE NEW VENTURE Information for developing the marketing plan may necessitate conducting some marketing research. Marketing research involves the gathering of data to determine such information as who will buy the product or service, what is the size of the potential market, what price should be charged,...

  • Excel file Homework1 - invoices.xlsx contains data about invoices of a reseller of office equipment that...

    Excel file Homework1 - invoices.xlsx contains data about invoices of a reseller of office equipment that ships its products to customers in several countries. The tables in the workbook refer to data collected from different systems within the company. a) Copy worksheet “InvoiceHeader” into a new worksheet. For each invoice in the new worksheet, show the matching information from the other tables: Invoice date, Invoice Number, Customer ID, Customer Name, Country, Invoice Quantity, Unit Cost, Unit Price. b) In the...

  • Please read the article and answer about questions. You and the Law Business and law are...

    Please read the article and answer about questions. You and the Law Business and law are inseparable. For B-Money, the two predictably merged when he was negotiat- ing a deal for his tracks. At other times, the merger is unpredictable, like when your business faces an unexpected auto accident, product recall, or government regulation change. In either type of situation, when business owners know the law, they can better protect themselves and sometimes even avoid the problems completely. This chapter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT