Computer >> Máy Tính >  >> Lập trình >> Java

Làm cách nào để đọc nội dung của tệp JSON bằng Java?

JSON hoặc JavaScript Object Notation là một tiêu chuẩn mở dựa trên văn bản nhẹ được thiết kế để trao đổi dữ liệu mà con người có thể đọc được. Các quy ước được sử dụng bởi JSON được các lập trình viên biết đến, bao gồm C, C ++, Java, Python, Perl, v.v. Tài liệu JSON mẫu -

{
   "book": [
      {
         "id": "01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },
      {
         "id": "07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      }
   ]
}

Thư viện Json-simple

Json-simple là một thư viện trọng lượng nhẹ được sử dụng để xử lý các đối tượng JSON. Sử dụng cái này, bạn có thể đọc hoặc viết nội dung của tài liệu JSON bằng chương trình Java.

JSON-Sự phụ thuộc maven đơn giản

Sau đây là phần phụ thuộc maven cho thư viện JSON-simple -

<dependencies>
   <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
   </dependency> 301 to 305
</dependencies>

Dán cái này vào thẻ ở cuối tệp pom.xml của bạn. (trước thẻ )

Ví dụ

Trước hết, hãy để chúng tôi tạo JSON tài liệu có tên sample.json với 6 cặp khóa-giá trị như được hiển thị bên dưới -

{
   "ID": "1",
   "First_Name": "Shikhar",
   "Last_Name": "Dhawan",
   "Date_Of_Birth": "1981-12-05",
   "Place_Of_Birth":"Delhi",
   "Country": "India"
}

Để đọc nội dung của tệp JSON bằng chương trình Java -

  • Khởi tạo lớp JSONParser của thư viện json-simple.
JSONParser jsonParser = new JSONParser();
  • Phân tích cú pháp nội dung của đối tượng thu được bằng cách sử dụng parse () phương pháp.
//Parsing the contents of the JSON file
JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/players_data.json"));
  • Truy xuất giá trị được liên kết với một khóa bằng cách sử dụng get () phương pháp.
String value = (String) jsonObject.get("key_name");

Chương trình Java sau phân tích cú pháp sample.json được tạo ở trên , đọc nội dung của nó và hiển thị chúng.

Ví dụ

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();
      }
   }
}

Đầu ra

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