Thursday, 2 November 2017

Create JSON Object And Array Using PHP MySQL

If you want to transfer your data to another client (mobile or server) you have to create your own JSON. JSON or JavaScript Object Notation is a file that used to transfer data between clients/server or between two different programming languages.

JSON have two types, they are :

  • JSON type Object
  • JSON type Array
What the different of these type?

JSON Type Object

JSON Type Object has characteristic always using mark {}, please take a look at the following sample json :

JSON type Array

We can call a json as an array is the JSON use the mark [] in its data. Why this json use the mark []? Because the data that will be send is more that one object. Please take a look at the following sample :

If you will transfer data more that one object, use the JSON type array. But, if you just transfer only one object, use JSON type Object as its format.

Create JSON Array using PHP and MySQL 

First, you have to preparing your data and has been saved in your MySQL database. In this project i will using data in my database and look like the follows :

Create your connection to connect the database and your project. Here is the code :


After you connection has works well. Now create your JSON type Array


Running the above file, and you will show your data as list in json.

Create JSON type Object Using PHP and MySQL

Here is the file that you can use to create JSON type Object.

The above json will catch data type object from android and will send its data to database. I have been implemented it and has been post the project in this blog. Please open this : Tutorial insert data from android using retrofit to Server (PHP + MySQL).

That's all. Hope this article helps you. Thank you.