React Native JSON Parsing ListView From MySQL Database Android iOS

In this tutorial we would going to learn about parsing JSON data in react native application using Fetch API networking library. Fetch API is one of the common API’s used by react native developers. So using this API we would retrieve the complete JSON object from MySQL database server. The MySQL database data is convert into JSON fromat using PHP. So let’s get started


Contents in this project JSON Parsing ListView :

 

1. Create Database + Table on your online hosting server or local server :

Create a fresh table inside your database named as FruitsNameListTable . The table has two columns id and fruit_name.

2. Insert Records into Table :

Next step is to insert some records into your table for example i am creating table to store different type of fruit names. Below is the screenshot of my table after inserting records.

3. Create PHP file code to retrieve data from MySQL database and convert into JSON.

Now create Two PHP files First one is DBConfig.php and second is FruitsList.php.

DBConfig.php :- This file contains your hosting server configuration like server host name, database name, database username and database password.

FruitsList.php :- This file fetch the MySQL records from table and convert them into JSON than ECHO them on screen, so they can be accessible.

Code for DBConfig.php file :

Code for FruitsList.php file :-

Output of JSON after running the URL should look like this :

4. Start a fresh React Native project. If you don’t know how then read my this tutorial.

5. Add
AppRegistry ,
StyleSheet ,
ActivityIndicator ,
ListView ,
Text ,
View ,
Alert  component in import block.

6. Create constructor in your class with props and set this.state isLoading: true.

isLoading: true  tells us whether our JSON data is loading from server or not so we can hide the Activity indicator.

7. Create a function to display the JSON listview selected item. The function name is GetItem with fruit_name parameter, than the function name should look this this : GetItem (fruit_name) .

This function is used to show the selected item from ListView in alert message.

8. Create
componentDidMount()  method. This is a inbuilt method.

9. Create Fetch API network request in
componentDidMount()  method.

Using Fetch method first we can pass our HTTP URL from which the JSON is coming. Then using response object we have hold the response coming from server. Now fill the JSON object items to ListView rows one by one.

10. Create
ListViewItemSeparator  method.

Using this method we would show a simple item separator line between ListView items.

11. Inside the
render()  block put a if condition checking on this.state.isLoading and if the this.state.isLoading is true then show the 
<ActivityIndicator /> .

12. Create custom css class named as MainContainer and rowViewContainer just above the AppRegistry.registerComponent line.

MainContainer : is for complete layout screen.

rowViewContainer : is to modify each ListView element.

13. Add View as parent view in render’s return block and Call the MainContainer class into View.

14. Add
ListView  component in View.

dataSource : Adding the JSON data into ListView.

renderSeparator : Display a border between items using
ListViewItemSeparator()  .

15. Complete source code for index.android.js / index.ios.js file.

Screenshots:

JSON