Hướng dẫn convert json string to list of dictionaries python - chuyển đổi chuỗi json thành danh sách từ điển python

19

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang gửi một chuỗi JSON từ Objective-C đến Python. Sau đó, tôi muốn chia nội dung của chuỗi vào danh sách Python. Tôi đang cố gắng lặp lại một chuỗi (bất kỳ chuỗi nào bây giờ):

import json

s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
jdata = json.loads(s)
for key, value in jdata.iteritems():
    print key, value

Tôi nhận được lỗi này:

Lỗi ngoại lệ: 'Danh sách' đối tượng không có thuộc tính 'lặp lại'

Hướng dẫn convert json string to list of dictionaries python - chuyển đổi chuỗi json thành danh sách từ điển python

Hỏi ngày 18 tháng 12 năm 2012 lúc 17:17Dec 18, 2012 at 17:17

Dữ liệu JSON của bạn là danh sách các từ điển, vì vậy sau json.loads(s), bạn sẽ có jdata làm danh sách, không phải là một từ điển.

Hãy thử một cái gì đó như sau:

import json

s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
jdata = json.loads(s)
for d in jdata:
    for key, value in d.iteritems():
        print key, value

Đã trả lời ngày 18 tháng 12 năm 2012 lúc 17:20Dec 18, 2012 at 17:20

Andrew Clarkandrew ClarkAndrew Clark

196K33 Huy hiệu vàng265 Huy hiệu bạc297 Huy hiệu Đồng33 gold badges265 silver badges297 bronze badges

json.loads(s) sẽ trả lại cho bạn list. Để lặp lại nó, bạn không cần

import json

s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
jdata = json.loads(s)
for d in jdata:
    for key, value in d.iteritems():
        print key, value
0.

>>> jdata = json.loads(s)
>>> for doc in jdata:
...     for key, value in doc.iteritems():
...          print key, value

Đã trả lời ngày 18 tháng 12 năm 2012 lúc 17:19Dec 18, 2012 at 17:19

Hướng dẫn convert json string to list of dictionaries python - chuyển đổi chuỗi json thành danh sách từ điển python

1

Đối với Python 3.6 ở trên, có một chút khác biệt

s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
jdata = json.loads(s)
print (jdata)
for d in jdata:
    for key, value in d.items():
        print (key, value)


[{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
i imap.gmail.com
p someP@ss
i imap.aol.com
p anoterPass

Đã trả lời ngày 24 tháng 5 năm 2017 lúc 8:00May 24, 2017 at 8:00

Hướng dẫn convert json string to list of dictionaries python - chuyển đổi chuỗi json thành danh sách từ điển python

Xiyuruixiyuruixiyurui

2153 Huy hiệu bạc4 Huy hiệu đồng3 silver badges4 bronze badges

1

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    Bàn luận : test_str = [“[{‘Gfg’ : 3, ‘Best’ : 8}, {‘Gfg’ : 4, ‘Best’ : 8}]”] Output : [[{‘Gfg’: 3, ‘Best’: 8}, {‘Gfg’: 4, ‘Best’: 8}]] Explanation : String converted to list of dictionaries. Input : test_str = [“[{‘Gfg’ : 3, ‘Best’ : 8}]”] Output : [[{‘Gfg’: 3, ‘Best’: 8}]] Explanation : String converted to list of dictionaries.

    Phương thức #1: & nbsp; sử dụng json.loads () + thay thế ()

    Sự kết hợp của các chức năng trên có thể được sử dụng để giải quyết vấn đề này. Trong đó, chúng tôi thay thế các chuỗi bên trong bằng cách sử dụng danh sách thay thế () và từ điển được tạo bằng tải ().

    Python3

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    1
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    2

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    3
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    4
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    5
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    6
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    7

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    8
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    9
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    0
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    1
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    2
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    3

    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    4
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    4
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    6
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    7
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    8
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    9
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    0
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    1
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    2

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    8
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    9
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    5
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    1
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    2
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    8

    Đầu ra

    The original string is : ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 9}]"]
    Converted list of dictionaries : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 9}]]

    Phương pháp số 2: Sử dụng Eval ()

    Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Eval (), đánh giá nội bộ kiểu dữ liệu và kết quả yêu cầu trả về.

    Python3

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    3
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    4
    The original string is : ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 9}]"]
    Converted list of dictionaries : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 9}]]
    1

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    8
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    9
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    0
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    1
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    2
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    3

    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    4
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    4
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    6
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    7
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    8
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    9
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    0
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    1
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    2

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    8
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    9
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    5
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    1
    >>> jdata = json.loads(s)
    >>> for doc in jdata:
    ...     for key, value in doc.iteritems():
    ...          print key, value
    
    2
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    print (jdata)
    for d in jdata:
        for key, value in d.items():
            print (key, value)
    
    
    [{'i': 'imap.gmail.com', 'p': 'someP@ss'}, {'i': 'imap.aol.com', 'p': 'anoterPass'}]
    i imap.gmail.com
    p someP@ss
    i imap.aol.com
    p anoterPass
    
    8

    Đầu ra

    The original string is : [{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 9, 'Best' : 9}]
    Converted list of dictionaries : [{'Gfg': 3, 'Best': 8}, {'Gfg': 9, 'Best': 9}]

    Phương pháp số 2: Sử dụng Eval ()

    Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Eval (), đánh giá nội bộ kiểu dữ liệu và kết quả yêu cầu trả về.O(n)

    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    3
    import json
    
    s = '[{"i":"imap.gmail.com","p":"someP@ss"},{"i":"imap.aol.com","p":"anoterPass"}]'
    jdata = json.loads(s)
    for d in jdata:
        for key, value in d.iteritems():
            print key, value
    
    4
    The original string is : ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 9}]"]
    Converted list of dictionaries : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 9}]]
    1
    O(n)


    JSON có phải là một danh sách từ điển không?

    JSON Định dạng JSON ở cấp cao nhất là từ điển của các cặp thuộc tính/giá trị hoặc các cặp khóa/giá trị như chúng ta đã nói về từ điển trong lớp này. Các giá trị là số, chuỗi, từ điển khác và danh sách. Dưới đây là một ví dụ đơn giản chỉ với 4 cặp thuộc tính/giá trị.JSON at its top-level is a dictionary of attribute/value pairs, or key/value pairs as we've talked about dictionaries in this class. The values are numbers, strings, other dictionaries, and lists. Here is a simple example with just 4 attribute/value pairs.

    Làm thế nào để bạn chuyển đổi một chuỗi thành một danh sách trong Python?

    Chuỗi Python là một chuỗi các ký tự.Chúng ta có thể chuyển đổi nó thành danh sách các ký tự bằng hàm tích hợp danh sách ().using list() built-in function.

    Làm thế nào để bạn có được JSON để tải vào một từ điển được đặt hàng trong Python?

    Chuỗi JSON đến từ điển Python Để thực hiện việc này, chúng tôi sẽ sử dụng hàm load () của mô -đun JSON, chuyển chuỗi làm đối số.json.Tải (data_json) tạo ra một từ điển mới với các cặp giá trị khóa của chuỗi JSON và nó trả về từ điển mới này.use the loads() function of the json module, passing the string as the argument. json. loads(data_JSON) creates a new dictionary with the key-value pairs of the JSON string and it returns this new dictionary.

    Làm cách nào để đọc một từ điển JSON trong Python?

    Hàm này được sử dụng để phân tích chuỗi JSON ...
    Cú pháp: json.load (file_name).
    Tham số: Nó lấy tệp JSON làm tham số ..
    Loại trả về: nó trả về đối tượng từ điển Python ..