Home > Zyx (Lain-lain) > JSON format

JSON format

Di kelas tadi, ada seorang mahasiswa yang bertanya tentang apa itu JSON. OK, inti ceritanya gini.

JSON singkatan dari JavaScript Object Notation, sebagai alternatif format standar penulisan array yang digunakan agar pertukaran data lebih mudah.

Contoh penulisan sebuah tabel:

Employees
+-----------+-----------+
| firstName | lastName  |
+-----------+-----------+
| John      | Doe       |
| Anna      | Smith     |
| Peter     | Jones     |
+-----------+-----------+

format JSON:

{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}

implementasi pada PHP:

$json_string='
     {"employees":[
         {"firstName":"John", "lastName":"Doe"},
         {"firstName":"Anna", "lastName":"Smith"},
         {"firstName":"Peter", "lastName":"Jones"}
     ]}';
$json=json_decode($json_string);
var_dump($json);

foreach($json->employees as $p) {
  $firstName = $p->firstName;
  $lastName = $p->lastName;
  echo $firstName." ".$lastName;
}

Silahkan mencoba😉

Categories: Zyx (Lain-lain) Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: