TheKodProgram PHP ve JAVA SCRIPT ile JSON Kullanımı
TheKodProgram PHP ve JAVA SCRIPT ile JSON Kullanımı

Hello everyone in this article we will talk about JSON and how to use it with Javascript and PHP. First of all, what is JSON?

What is JSON ?

  • Stands for Java Script Object Notation.
  • Very Fast It is a data transport system.
  • It is not dependent on any language.
  • It is easy to understand, create and write.

JSON is a form of an array, called JavaScript, which indicates that it was prepared with that logic. JSON is a system available in all languages.

Now let’s give an example JSON array below to see the structure.


I have created a sample JSON array as we can see in this example …

JSON Data is generated with Java Script Logic.

When creating JSON data, we need to observe the following rules.

  • JSON data name – Consists of value pairs
  • Created name – value pairs separated by commas
  • Ornamental brackets hold this name – value pairs
  • The square brackets hold the fancy brackets.

Now let’s examine them in order

We can think of it as a kind of property – value.

When creating, ” ” must be enclosed in quotation marks.

As an example

An example is a JSON name value pair.
Now let’s create a sample JSON array using them.


Now let’s learn to send arrays into JSON. In the following example we will look at how the directory is in JSON …

As we have seen above, we keep the arrays with angular warrants and keep them in a json array again.

We kept the course names and the names of their teachers under the series of courses. We kept the classes inside a JSON array as if it were a JSON object.

We can read JSON data with Javascript, while JSON.parse can read it.


Usage JSON with PHP

Now let’s see how to use JSON data with PHP.

Firstly, after PHP 5.2, JSON data can be read directly in PHP. We need to know that in lower versions will not work.

First, let’s look at the JSON functions in PHP.

  • json_encode: Write a prepared array in JSON format
  • json_last_error: Checks if there is an error when executing a JSON function
  • json_decode: Decodes an existing JSON array and makes it understandable to PHP.

JSON_ENCODE method : 

With this function we can convert an array to JSON format. It is very simple to use. We created a JSON object now.

JSON_DECODE method:

With this function, we can decode and understand an existing JSON array. So we read JSON data.


Above we can decode and read our JSON object … Now let’s see how we can communicate between each other.

Let’s have a sample PHP file and get JSON data from another php page using AJAX and print it

first create JSON data on the back page with PHP, then call the data from the front with AJAX, then parse it and print it on the screen.

Below the PHP code that created the JSON object:


In this way, we read the data posted on this page and output it in JSON form. Now let’s see how to post in front and read the incoming data and finish our article …

As you can see, we have moved the JSON object between Ajax and PHP Script using Ajax and printed our data on the front page.

That’s all in this article.

Keep following me.

Have good coding.

Burak Hamdi TUFAN

LEAVE A REPLY

Please enter your comment!
Please enter your name here