[Android] JSon trong Android – Part 1: JSON and JSON output in php

Hello everyone, Today is the first of a series JSon trong Android, Like the previous series in the Android Database, I will guide you to do 1 app specific hilarious from start to finish using json interact with databases on the web server – App store list exes =)).

This series you should have some basic knowledge concerning as MySQL, Php.
The first article I'll briefly introduce JSON and JSON output in php

What is JSON, Why use JSON

JSON it stands for JavaScript Object Notation, it is essentially a certain chain structure, eg structured in: https://www.cachhoc.net/mobile/demo-json/lover.php?test=true&nick = related

[
    {
        "id": "1",
        "nick": "quan",
        "name": "Nguyen Hoang Hau",
        "begin_date": "20\/10\/2010",
        "end_date": "19\/11\/2010",
        "phone": "0987654321"
    },
    {
        "id": "2",
        "nick": "quan",
        "name": "Cao Thi Thap",
        "begin_date": "20\/11\/2010",
        "end_date": "14\/02\/2011",
        "phone": "1234567890"
    },
    {
        "id": "3",
        "nick": "quan",
        "name": "L\u00f2 L\u1ea5p L\u00f3",
        "begin_date": "08\/03\/2011",
        "end_date": "01\/06\/2011",
        "phone": "0987612345"
    }
]

JSON structure could begin with a square brackets [] as above, Meanwhile it performed an array of elements inside (above the array 3 part from). Or, it could start with braces {}, Meanwhile it performing an element and each element contains key-value pairs (keys and values), as you can see on “id”:”2″ key-value is a pair. Or JSON as well maybe just a key-value pair or a very ordinary chain. But here I wanted the attention and differentiate you in pairs Crouching JSON square and curly brackets because their treatment is very different (An array of element and one element).

We can fully use the xml or some other structure for formatting and data transfer but with JSON format it is a compact and easy to use, is the trend now.

How to export from the web server with php JSON

To be able to practice the JSON output from the web server, you need a server (host) private. If you do not have your own host can fully use the service free hosting provider php and MySQL support as Hostinger, you only need to register an account, create 1 Free website on which (you can easily do this with the help of Google) and we were able to begin.

To export the data in php we have many ways but I usually use the following command to export because it will output immediately visible structured format:

$json = json_encode($result, JSON_PRETTY_PRINT);
print_r($json);

However on his Hostinger inexplicably JSON_PRETTY_PRINT use should not be out in this production only json_encode($result).
In that $ result is a variable data, specifically a simple example created 1 file temp.php (Put a file manager to create as normal files):

<?php 
	$result = array();
	$result['name'] = 'nguyenvanquan7826';
	$result['website']	= 'https://cachhoc.net';
	$json = json_encode($result);
	print_r($json);
?>

When you run this file will be opening paragraph is json 1 current:

{"name":"nguyenvanquan7826","website":"https:\/\/cachhoc.net"}

check: http://svictu.esy.es/mobile/old-lover/temp.php

If json not visible structured products you can copy it to the page http://jsonviewer.stack.hu/ and select format to look for easy.

When outputting json, Can you see it font error (like for example the first post, second element 3 may name is equivocal but faulty furnaces font) or have some special characters (as link http:// dill http:\/\/), This what you feel secure because it is the json format, to the series down to android processor will not alright.

End section 1 here, the next section I will guide you to create Database and JSON output from Database.

Posts made in the tutorial JSon trong Android by nguyenvanquan7826