Android Quiz 3
Android Quiz 3
M Ahmad Tahir
Department: -
Computer Science
Subject: -
Mobile Application Development
Semester: -
8th
Roll No: -
FC-072
Submitted to: -
Ms. Samra Tariq
Work: -
Quiz 3
What is a JSON file?
JSON, which stands for JavaScript Object Notation, is a lightweight, text-based, open
standard format designed for human-readable data interchange. It was derived from
JavaScript but is a language-independent format, meaning it can be used with many
different programming languages. JSON is commonly used for transmitting data
between a server and a web application (e.g., sending data from the server to be
displayed on a webpage, or vice versa).
1. Objects:
o An unordered collection of key/value pairs.
o Begins with a left curly brace { and ends with a right curly brace }.
o Each key is a string enclosed in double quotes (").
o Each key is followed by a colon :.
o Values can be strings, numbers, booleans (true or false), arrays, other
JSON objects, or the null value.
o Key/value pairs are separated by commas ,.
o Keys within an object should ideally be unique.
o The order of key/value pairs in an object is not significant.
2. Arrays:
o An ordered list of values.
o Begins with a left square bracket [ and ends with a right square
bracket ].
o Values are separated by commas ,.
o Array elements can be of any valid JSON data type (strings, numbers,
booleans, objects, other arrays, or null).
Use Cases
JSON is widely used in various applications due to its simplicity and ease of parsing:
1. Web Development (APIs): It's the most common format for data exchange
between web clients (browsers) and servers. RESTful APIs frequently use JSON
to send and receive data.
2. Configuration Files: Many applications use JSON to store configuration
settings because it's easy for both humans and machines to read and write.
3. Data Storage: While not a database itself, JSON is often used to structure data
that is then stored in NoSQL databases like MongoDB, which use JSON-like
document formats (BSON).
4. Data Serialization: Converting data structures or objects in a programming
language into a format that can be stored or transmitted, and then
reconstructed later.
5. Logging: Storing log data in JSON format makes it easier to parse, search, and
analyze.
6. Inter-Process Communication: Exchanging data between different software
applications or components.
How you can read data from a JSON file explain with the help of
code.
How to Read Data from a JSON File
Reading data from a JSON file typically involves opening the file, reading its content as a
string, and then parsing that string into a data structure native to the programming language
being used
MainActivity.java
package com.example.json;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayText.append(" - ").append(courseName).append("
(").append(credits).append(" credits)\n");
Log.i(TAG, " - Course: " + courseName + ", Credits: " +
credits);
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/json_data_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading JSON data..."
android:textSize="16sp" />
</LinearLayout>
Sample.json
{
"studentName": "Wahab Noman",
"studentID": "FC076",
"age": 21,
"isEnrolled": true,
"courses": [
{
"courseName": "Android Development",
"credits": 4
},
{
"courseName": "Economics In Computing",
"credits": 2
},
{
"courseName": "Parallel & Distributive Computing",
"credits": 2
}
],
"contact": {
"email": "[email protected]",
"phone": "0000000000"
}
}
Output