Hướng dẫn update data in json python - cập nhật dữ liệu trong json python

Tôi đang cố cập nhật tệp JSON hiện có, nhưng từ một số lý do, giá trị được yêu cầu không được thay đổi nhưng toàn bộ tập hợp các giá trị (với giá trị mới) đang được thêm vào tệp gốc

Show
jsonFile = open("replayScript.json", "r+")
data = json.load(jsonFile)


tmp = data["location"]
data["location"] = "NewPath"

jsonFile.write(json.dumps(data))

và kết quả là: Yêu cầu:

{
   "location": "NewPath",
   "Id": "0",
   "resultDir": "",
   "resultFile": "",
   "mode": "replay",
   "className":  "",
   "method":  "METHOD"
}

Actual:

{
"location": "/home/karim/storm/project/storm/devqa/default.xml",
"Id": "0",
"resultDir": "",
"resultFile": "",
"mode": "replay",
"className":  "",
"method":  "METHOD"
}
{
    "resultDir": "",
    "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
    "method": "METHOD",
    "className": "",
    "mode": "replay",
    "Id": "0",
    "resultFile": ""
}

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
     

    Bàn luận  

    • Hình thức đầy đủ của JSON là 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 trích dẫn có chứa giá trị trong ánh xạ giá trị khóa trong {}. & Nbsp; & nbsp;json.loads() function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    Các chức năng được sử dụng: & nbsp; & nbsp; json.loads(json_string)
    Parameter: It takes JSON string as the parameter.
    Return type: It returns the python dictionary object. 
     

    • json.loads (): 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;json.dumps() function is present in python built-in ‘json’ module. This function is used to convert Python object into JSON string.
       

    Cú pháp: tham số JSON.LOADS (JSON_STRING): Nó lấy chuỗi JSON làm tham số.return loại: nó trả về đối tượng từ điển Python. & Nbsp; & nbsp; json.dumps(object)
    Parameter: It takes Python Object as the parameter.
    Return type: It returns the JSON string. 
     

    • CẬP NHẬT (): Phương thức này cập nhật từ điển với các phần tử từ một đối tượng từ điển khác hoặc từ một cặp khóa/giá trị có thể lặp lại. & NBSP; This method updates the dictionary with elements from another dictionary object or from an iterable key/value pair.
       

    Cú pháp: Dict.Update ([Khác]) Tham số: Lấy một từ điển khác hoặc một cặp khóa/giá trị có thể lặp lại. dict.update([other])
    Parameters: Takes another dictionary or an iterable key/value pair.
    Return type: Returns None. 
     

    Ví dụ 1: Cập nhật chuỗi JSON. & NBSP; & NBSP; Updating a JSON string.
      

    Python3

    import json

    ________ 5 ________ 6 & nbsp; Các

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    2
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    3:
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    5
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    1

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    2
    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    8:
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    0
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    1

    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    2=
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    4
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    5:
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    7
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    8

    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    9= import1

    import2

    import3import4

    Đầu ra: & nbsp;
     

    {Pin Pin Cảnh: 110096, Tổ chức của người Hồi giáo
     

    Ví dụ 2: Cập nhật tệp JSON. Giả sử tệp JSON trông như thế này. & NBSP; Updating a JSON file. Suppose the JSON file looks like this.
     

    Hướng dẫn update data in json python - cập nhật dữ liệu trong json python

    Chúng tôi muốn thêm một dữ liệu JSON khác sau EMP_DETAILS. Dưới đây là việc thực hiện.

    Python3

    import json

    ________ 5 ________ 6 & nbsp; Các

    json2json3json4json5json6json7json8:

    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    2=
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    4
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    5:
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    7
    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    8

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    2x 7x 8x 9

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    2json8=2=3x 5

    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    9= import1

    Đầu ra: & nbsp;

    '{ 9"organization"0"organization"1"organization"2

    {
       "location": "NewPath",
       "Id": "0",
       "resultDir": "",
       "resultFile": "",
       "mode": "replay",
       "className":  "",
       "method":  "METHOD"
    }
    
    1

    {Pin Pin Cảnh: 110096, Tổ chức của người Hồi giáo

    json2

    {
    "location": "/home/karim/storm/project/storm/devqa/default.xml",
    "Id": "0",
    "resultDir": "",
    "resultFile": "",
    "mode": "replay",
    "className":  "",
    "method":  "METHOD"
    }
    {
        "resultDir": "",
        "location": "pathaaaaaaaaaaaaaaaaaaaaaaaaa",
        "method": "METHOD",
        "className": "",
        "mode": "replay",
        "Id": "0",
        "resultFile": ""
    }
    
    8

    :0

    Output: 
     

    Hướng dẫn update data in json python - cập nhật dữ liệu trong json python