Kinh Nghiệm về Show json format in html Đầy đủ Mới Nhất

Bạn đang tìm kiếm từ khóa Show json format in html Đầy đủ được Cập Nhật vào lúc : 2022-10-05 21:40:00 . Với phương châm chia sẻ Thủ Thuật Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi Read nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Mình lý giải và hướng dẫn lại nha.

Mẹo Hướng dẫn Show json format in html Chi Tiết

Pro đang tìm kiếm từ khóa Show json format in html được Cập Nhật vào lúc : 2022-10-05 21:40:25 . Với phương châm chia sẻ Bí kíp Hướng dẫn trong nội dung nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn hoàn toàn có thể lại Comments ở cuối bài để Mình lý giải và hướng dẫn lại nha.

For the syntax highlighting, use code prettify. I believe this is what StackOverflow uses for its code highlighting.

Nội dung chính

    Fetching the JSON data
    Displaying the JSON data
    Why use Vanilla JavaScript?
    How do I display a JSON table in HTML?
    How show JSON data in HTML DIV?
    Can I put JSON in HTML?
    How do I display JSON data in my browser?

Wrap your formatted JSON in code blocks and give them the “prettyprint” class.
Include prettify.js in your page.
Make sure your document’s body toàn thân toàn thân tag calls prettyPrint() when it loads

You will have syntax highlighted JSON in the format you

have laid out in your page. See here for an example. So if you had a code block like this:

var jsonObj =

“height” : 6.2,

“width” : 7.3,

“length” : 9.1,

“color” :

“r” : 255,

“g” : 200,

“b” : 10

It would look like this:

var jsonObj =

“height” : 6.2,

“width” : 7.3,

“length” : 9.1,

“color” :

“r” : 255,

“g” : 200,

“b” : 10

This doesn’t help with the indenting, but the other answers seem to be addressing that.

In this tutorial, I will show you how to fetch and display data from a JSON file using vanilla JavaScript.

So how will we achieve this?

First, we will fetch the JSON data by using the fetch API. This will return

a promise with our JSON data. Then we will append the data dynamically by creating HTML elements on the fly. We will then append our JSON data to those elements.

Getting JSON data from an API and display it on a web page is a common thing you will do quite often. I have created similar posts on the big frameworks like React,

Vue and Angular. Check it out if you are using any of those frameworks.

Let’s get started!

First, create a people.json file and fill it with the following data:

[

“id”: “1”,

“firstName”: “John”,

“lastName”: “Doe”

,

“id”: “2”,

“firstName”: “Mary”,

“lastName”: “Peterson”

,

“id”: “3”,

“firstName”: “George”,

“lastName”: “Hansen”

]

We will save this file in the same directory as our index.html file.

Fetching the JSON data

To be able to display this data in our HTML file, we first need to fetch the data with JavaScript.

We will fetch this data by using the fetch API. We use the fetch API in the following way:

fetch(url)

.then(function (response)

// The JSON data will arrive here

)

.catch(function (err)

// If an error occured, you will catch it here

);

The url parameter used in the fetch function is where we get the JSON data. This is often an address. In our case it is just the filename people.json. We don’t have to drill down to any directory since the json file is in the same directory as ourindex.html.

The fetch function will return a promise. When the JSON data is fetched from the file, the then function will run with the JSON data in the response.

If anything goes wrong (like the JSON file cannot be found), the catch function will run.

Let us see how this will look in out example:

fetch(‘people.json’)

.then(function (response)

return response.json();

)

.then(function (data)

appendData(data);

)

.catch(function (err)

console.log(err);

);

Here we are fetching our

people.json file. After the file has been read from disk, we run the then function with the response as a parameter. To get the JSON data from the response, we execute the json() function.

The json() function also returns a promise. This is why we just return it and chain another then function. In the second then function we get the actual JSON data as a parameter. This data looks just like the data in our JSON file.

Now we can take this data and display it on our HTML

page. Notice that we are calling a function called appendData. This is where we create the code which will append the data to our page.

Notice that in our catch function, we are just writing the error message to out console. Normally you would display an error message to the user if something went wrong.

Displaying the JSON data

Before we display our JSON data on the webpage,

let’s just see how the body toàn thân toàn thân of our index.html file looks like.

Pretty simple right? We have just a simple div with the id myData. Our plan is to fill our JSON data inside this div dynamically.

There are several ways to display the data in our HTML. We could create a table and make it look really good with nice styling. However, we will do this in a simple and ugly way.

Our goal is to just simply display the full name of the people in our JSON file.

Step 1 – Get the div element from the body toàn thân toàn thân

Remember the div with the myData id from ourindex.html? We want to fetch that div using JavaScript. We need this because we are going to fill the div with the people in our JSON file.

This is how we will do it:

var mainContainer = document.getElementById(“myData”);

We get the element by executing the getElementByID function. This will find the

element with the id myData. This is vanilla JavaScript and this is how we did front-end development in the “old” days :).

Step 2 – Loop through every object in our JSON object

Next step is to create a simple loop. We can then get every object in our list of JSON object and append it into our main div.

for (var i = 0; i < data.length; i++)

// append each person to our page

Step 3 – Append each person to our HTML

page

Inside the for-loop we will append each person inside its own div. This code will be repeated three times for each person.

First, we will create a new div element:

var div = document.createElement(“div”);

Next we will fill that div with the full name from our JSON file.

div.innerHTML = ‘Name: ‘ + data[i].firstName + ‘ ‘ + data[i].lastName;

Lastly, we will append this div to our main container:

mainContainer.appendChild(div);

That’s it. Now we have finished appending the JSON data to our index.html page. The

full appendData function looks like this:

function appendData(data)

var mainContainer = document.getElementById(“myData”);

for (var i = 0; i < data.length; i++)

var div = document.createElement(“div”);

div.innerHTML = ‘Name: ‘ + data[i].firstName + ‘ ‘ + data[i].lastName;

mainContainer.appendChild(div);

When we run our index.html page, it will look something like this:

Not the most beautiful application, but it got the job done.

Let us

look the entire HTML file with the JavaScript:

JSON Test

fetch(‘people.json’)

.then(function (response)

return response.json();

)

.then(function (data)

appendData(data);

)

.catch(function (err)

console.log(‘error: ‘ + err);

);

function appendData(data)

var mainContainer = document.getElementById(“myData”);

for (var i = 0; i < data.length; i++)

var div = document.createElement(“div”);

div.innerHTML = ‘Name: ‘ + data[i].firstName + ‘ ‘ + data[i].lastName;

mainContainer.appendChild(div);

Try to copy and paste this in your own editor. As an exercise, you can try to style the output to look nicer. Remember to include the people.json file as well. This file must be in the same directory as your index.html file for this to work.

Why use Vanilla JavaScript?

You might be wondering what

is the point of creating this in vanilla JavaScript. Doesn’t modern web application use frameworks and libraries like Angular, ReactJS or VueJS?

Well, yeah, you are probably right, most of the time. But some web pages are just static with very little logic.

If you just want to tweak some minor parts of the website, it might be overkill to include big libraries which will slow down the site.

Besides, frameworks and libraries come and go. Good old vanilla JavaScript is here to

stay. So take every opportunity to learn it, you don’t know when you might need it.

Happy coding!

How do I display a JSON table in HTML?

Approach 1:. Take the JSON Object in a variable.. Call a function which first adds the column names to the

element. (It is looking for the all columns, which is UNION of the column names).. Traverse the JSON data and match key with the column name. … . Leave the column empty if there is no value of that key..

How show JSON data in HTML DIV?

How to display JSON data in a div when JSON data is in Array? Answer: You can use for it to loop thru the array and construct an HTML string. Use jQuery ‘s . append() to add the string to the body toàn thân toàn thân.

Can I put JSON in HTML?

JSON can very easily be translated into JavaScript. JavaScript can be used to make HTML in your web pages.

How do I display JSON data in my browser?

Right click on JSON file, select open, navigate to program you want open with(notepad). Consecutive opens automatically use notepad.

Tải thêm tài liệu liên quan đến nội dung nội dung bài viết Show json format in html

programming

html

HTML to JSON

JSON HTML Formatter

Pre JSON HTML

String to JSON

Reply
1
0
Chia sẻ

Share Link Down Show json format in html miễn phí

Bạn vừa Read Post Với Một số hướng dẫn một cách rõ ràng hơn về Clip Show json format in html tiên tiến và phát triển và tăng trưởng nhất Chia SẻLink Tải Show json format in html miễn phí.

Hỏi đáp vướng mắc về Show json format in html

Nếu sau khi đọc nội dung nội dung bài viết Show json format in html vẫn chưa hiểu thì hoàn toàn hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha

#Show #json #format #html

Related posts:

4195

Clip Show json format in html Đầy đủ ?

Bạn vừa Read nội dung bài viết Với Một số hướng dẫn một cách rõ ràng hơn về Clip Show json format in html Đầy đủ tiên tiến và phát triển nhất

Chia Sẻ Link Tải Show json format in html Đầy đủ miễn phí

Heros đang tìm một số trong những ShareLink Tải Show json format in html Đầy đủ miễn phí.

Hỏi đáp vướng mắc về Show json format in html Đầy đủ

Nếu sau khi đọc nội dung bài viết Show json format in html Đầy đủ vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha
#Show #json #format #html #Đầy #đủ