Read Any Type Of JSON File in JAVA | Simple Introduction And Demo Of JavaScript Object Notation
Simple
Explanation Of JSON
Agenda :
1. What is JSON
2. Uses of JSON
3. Data Types of JSON
4. Characteristics of
JSON
5. Simple Example
1. What is JSON
- JSON-Java Script Object
Notation
- JSON is syntax for storing
and exchanging data.
- Basically, it was designed
for human-readable data interchange
- It has been extended from
the JavaScript language
- The filename extension is.
json
- JSON internet media type is
application/json
2. Uses Of JSON
- Helps you to transfer data
from server
- It is widely used for JavaScript-based
applications that include browser add-ons
- Web services and APIs use
the JSON format to get public data
- You can transmit the data
between server and web application using JSON
3. Data Types of JSON
- Number
- String
- Boolean
- Object
- Null
- Array
4. Characteristics of
JSON
- JSON is easy to read and
write
- It is lightweight text-based
interchange format
- JSON is language independent
5. Simple Example
JSON Input:
{
"firstName"
: "Sam",
"lastName"
: "Ray",
"address"
:
[
{
"street"
: "vadgaon
road",
"city"
: "pune",
"state"
: "MH"
},
{
"street"
: "Dehu
road",
"city"
: "pune",
"state"
: "MH"
}
]
}
Program:
package com.techhelpline.JSON;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import
org.json.simple.parser.ParseException;
public class ReadJSONFile {
public static void main(String[] args) throws IOException, ParseException {
FileReader jsonReader=new FileReader("./JSONFolder/employeeDetails.json");
JSONParser jsonParser=new JSONParser();
Object object=jsonParser.parse(jsonReader);
JSONObject jsonObject=(JSONObject) object;
String fName=(String) jsonObject.get("firstName");
String lName=(String) jsonObject.get("lastName");
System.out.println("fName: "+fName);
System.out.println("lName: "+lName);
JSONArray array=(JSONArray) jsonObject.get("address");
for (int i = 0; i < array.size(); i++) {
JSONObject
addressObject=(JSONObject) array.get(i);
String street=(String) addressObject.get("street");
String city=(String) addressObject.get("city");
String state=(String) addressObject.get("state");
System.out.println("Address of "+i+ "object");
System.out.println("street:"+street+", city:
"+city+", state: "+state);
}
}
}
Comments
Post a Comment