Hướng dẫn python read json file into dict - python đọc tệp json vào dict

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc
    Function Used: 

    • Bàn luậnjson.loads[] function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    JSON là viết tắt của ký hiệu đối tượng JavaScript. Điều đó có nghĩa là một tệp tập lệnh [thực thi] được tạo bằng văn bản bằng ngôn ngữ lập trình, được sử dụng để lưu trữ và chuyển dữ liệu. Python hỗ trợ JSON thông qua gói tích hợp có tên JSON. Để sử dụng tính năng này, chúng tôi nhập gói JSON trong tập lệnh Python. Văn bản trong JSON được thực hiện thông qua chuỗi được trích dẫn có chứa giá trị trong ánh xạ giá trị khóa trong {}. Nó tương tự như từ điển trong python.function được sử dụng: & nbsp; json.load[file_name]
    Parameter: It takes JSON file as the parameter.
    Return type: It returns the python dictionary object. 
     

    json.load []: hàm json.loads [] có mặt trong mô-đun python tích hợp ‘json. Hàm này được sử dụng để phân tích chuỗi JSON. & NBSP; Let’s suppose the JSON file looks like this:
     

    Cú pháp: JSON.LOAD [FILE_NAME] Tham số: Nó lấy tệp JSON làm tham số.return loại: nó trả về đối tượng từ điển Python. & Nbsp; & nbsp;
     

    Python3

    Ví dụ 1: Hãy giả sử tệp JSON trông như thế này: & nbsp;

    with open['data.json'] as json_file:

    Chúng tôi muốn chuyển đổi nội dung của tệp này thành từ điển Python. Dưới đây là việc thực hiện. & NBSP;

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    6
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    7
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    8
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    9

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[import3import4import5import6

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[json0import4json2import6

    import json
     

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    0____11
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    2
    Reading nested data 
    In the above JSON file, there is a nested dictionary in the first key people1. Below is the implementation of reading nested data.
     

    Python3

    Ví dụ 1: Hãy giả sử tệp JSON trông như thế này: & nbsp;

    with open['data.json'] as json_file:

    Chúng tôi muốn chuyển đổi nội dung của tệp này thành từ điển Python. Dưới đây là việc thực hiện. & NBSP;

    import json

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[open5open6

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    0____11
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    2

    [4

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[[7[8[9import6

    [4

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4['data.json'4[8'data.json'6import6

    [4

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[] as json_file:1[8] as json_file:3import6

    [4

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4] as json_file:7

    import json
     



     

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    0____11
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    33

    Chúng ta có thể chuyển đổi JSON thành Từ điển trong Python không?
     

    Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng phương thức JSON.LOADS []. Kết quả sẽ là một từ điển Python.

    Làm thế nào trích xuất dữ liệu cụ thể từ JSON trong Python?
     

    Đối tượng JSONĐối tượng Python
    sự vậtDIGN
    mảngdanh sách
    sợi dâystr
    vô giá trịKhông có
    Số [int]int
    số [thực]trôi nổi
    thậtĐÚNG VẬY
    saiSai

    json.load []: json.load [] chấp nhận đối tượng tệp, phân tích dữ liệu JSON, điền từ điển Python với dữ liệu và trả lại cho bạn. & nbsp; json.load[] accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.
     

    Syntax:

    json.load[file object]

    Ví dụ: Giả sử tệp JSON trông như thế này: Suppose the JSON file looks like this:

    Chúng tôi muốn đọc nội dung của tệp này. Dưới đây là việc thực hiện.

    Python3

    import json

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    0____11     8

    open8 open9[0 [1

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    03[3

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    07

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    08

    Output:

    JSON.LOADS []: Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng json.loads [] method.json.loads [] không lấy đường dẫn tệp, mà là nội dung tệp làm chuỗi, sử dụng FileObject. đọc [] với json.loads [] chúng ta có thể trả về nội dung của tệp. & nbsp; If you have a JSON string, you can parse it by using the json.loads[] method.json.loads[] does not take the file path, but the file contents as a string, using fileobject.read[] with json.loads[] we can return the content of the file.
     

    Syntax:

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject

    Ví dụ: Ví dụ này hiển thị đọc từ cả tệp chuỗi và JSON. Các tập tin hiển thị ở trên được sử dụng. This example shows reading from both string and JSON file. The file shown above is used.

    Python3

    import json

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    0____11     8

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4[
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    19
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    20

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4] as json_file:7

    open8 open9[0 [1

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    03[3

    JSON.LOADS []: Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng json.loads [] method.json.loads [] không lấy đường dẫn tệp, mà là nội dung tệp làm chuỗi, sử dụng FileObject. đọc [] với json.loads [] chúng ta có thể trả về nội dung của tệp. & nbsp;

    open8 open9[0 [1

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    03[3

        

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    4
    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    07

    json.loads[jsonstring] #for Json string
    
    json.loads[fileobject.read[]] #for fileobject
    08

    Output:


    Chúng ta có thể chuyển đổi JSON thành Từ điển trong Python không?

    Nếu bạn có chuỗi JSON, bạn có thể phân tích nó bằng cách sử dụng phương thức JSON.LOADS [].Kết quả sẽ là một từ điển Python.you can parse it by using the json.loads[] method. The result will be a Python dictionary.

    Làm thế nào trích xuất dữ liệu cụ thể từ JSON trong Python?

    Chuyển đổi tệp JSON thành đối tượng Python ngoài hàm JSON, hàm Open [] gốc của Python cũng sẽ được yêu cầu.Thay vì phương thức tải JSON, đọc các chuỗi JSON, phương thức được sử dụng để đọc dữ liệu JSON trong tệp là load [].Phương thức Load [] chiếm một đối tượng tệp và trả về dữ liệu JSON được phân tích cú pháp vào một đối tượng Python.load[]. The load[] method takes up a file object and returns the JSON data parsed into a Python object.

    Bài Viết Liên Quan

    Chủ Đề