JSON หรือ JavaScript Object Notation เป็นมาตรฐานเปิดแบบข้อความที่มีน้ำหนักเบาซึ่งออกแบบมาสำหรับการแลกเปลี่ยนข้อมูลที่มนุษย์สามารถอ่านได้ โปรแกรมเมอร์รู้จักอนุสัญญาที่ใช้โดย JSON ซึ่งรวมถึง C, C++, Java, Python, Perl เป็นต้น ตัวอย่างเอกสาร JSON −
{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
} ไลบรารี Json-simple
json-simple เป็นไลบรารี่น้ำหนักเบาซึ่งใช้ในการประมวลผลอ็อบเจ็กต์ JSON คุณสามารถอ่านหรือเขียนเนื้อหาของเอกสาร JSON โดยใช้โปรแกรม Java ได้
JSON-การพึ่งพา maven อย่างง่าย
ต่อไปนี้คือการพึ่งพา maven สำหรับไลบรารีแบบง่าย JSON -
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> 301 to 305 </dependencies>
วางสิ่งนี้ด้วยในแท็ก
ตัวอย่าง
ก่อนอื่น ให้เราสร้าง JSON เอกสารชื่อ sample.json ด้วยคู่คีย์-ค่า 6 คู่ดังที่แสดงด้านล่าง −
{
"ID": "1",
"First_Name": "Shikhar",
"Last_Name": "Dhawan",
"Date_Of_Birth": "1981-12-05",
"Place_Of_Birth":"Delhi",
"Country": "India"
} หากต้องการอ่านเนื้อหาของไฟล์ JSON โดยใช้โปรแกรม Java -
- สร้างตัวอย่างคลาส JSONParser ของไลบรารี json-simple
JSONParser jsonParser = new JSONParser();
- แยกวิเคราะห์เนื้อหาของวัตถุที่ได้รับโดยใช้ parse() วิธีการ
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json")); - ดึงค่าที่เกี่ยวข้องกับคีย์โดยใช้ get() วิธีการ
String value = (String) jsonObject.get("key_name"); โปรแกรม Java ที่ติดตามจะแยกวิเคราะห์ไฟล์ที่สร้าง sample.json . ด้านบน ไฟล์ อ่านเนื้อหา และแสดงผล
ตัวอย่าง
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadingJSON {
public static void main(String args[]) {
//Creating a JSONParser object
JSONParser jsonParser = new JSONParser();
try {
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/sample.json"));
String id = (String) jsonObject.get("ID");
String first_name = (String) jsonObject.get("First_Name");
String last_name = (String) jsonObject.get("Last_Name");
String date_of_birth = (String) jsonObject.get("Date_Of_Birth");
String place_of_birth = (String) jsonObject.get("Place_Of_Birth");
String country = (String) jsonObject.get("Country");
//Forming URL
System.out.println("Contents of the JSON are: ");
System.out.println("ID :"+id);
System.out.println("First name: "+first_name);
System.out.println("Last name: "+last_name);
System.out.println("Date of birth: "+date_of_birth);
System.out.println("Place of birth: "+place_of_birth);
System.out.println("Country: "+country);
System.out.println(" ");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
} ผลลัพธ์
Contents of the JSON are: ID :1 First name: Shikhar Last name: Dhawan Date of birth :1981-12-05 Place of birth: Delhi Country: India