python用反斜杠读取json文件

x33g5p2x  于 2023-03-24  发布在  Python
关注(0)|答案(2)|浏览(134)

我有一个json文件,数据是这样的

{\"Status\":\"OK\",\"isSuccess\":true,\"Error\":\"\",\"Data\":[{\"STOK_KODU\":\"REMA-50100\",\"BR1_PAYDA\":1,\"MARKA\":\"REMARK\",\"URETICI_KODU\":\"50100\",\"KDV_ORANI\":18.0,\"STOK_DURUM\":\"3ARA\",\"IMG\":1,\"STOK_TEXT\":\"REM03@FAN MOTORU 24VANELI  7\\\"@@ REMARK@ @@ XRFM1103@ 009 2985@ 2394009@ RF03\",\"ORAN1\":43.00000000,\"ORAN2\":7.00000000,\"ORAN3\":0.00000000,\"ORAN4\":0.0,\"ORAN5\":0.0,\"ORAN6\":0.0,\"ORAN7\":0.0,\"SATIS1\":9067.9},{\"STOK_KODU\":\"REMA-03\",\"BR1_PAYDA\":1,\"MARKA\":\"REMARK\",\"URETICI_KODU\":\"503\",\"KDV_ORANI\":18.0,\"STOK_DURUM\":\"3ARA\",\"IMG\":1,\"STOK_TEXT\":\"R103@MARS MOTORU 24V75 KW DELC955 D STR3@@ REMARK@ @TYERA/BR23/ @ XRE0103@ @ 2313@ 50103\",\"ORAN1\":43.00000000,\"ORAN2\":7.00000000,\"ORAN3\":0.00000000,\"ORAN4\":0.0,\"ORAN5\":0.0,\"ORAN6\":0.0,\"ORAN7\":0.0,\"SATIS1\":9067.9}]}

正如您所看到的,反斜杠有一些问题
我试着这样读

import json

with open("4.json", encoding="utf8") as file:
    reader = json.load(file)
    print(reader["Status"])

但我得到的错误,因为反斜杠,所以我的问题是,有办法读取这个文件,并访问其中的数据?

gijlo24d

gijlo24d1#

你能做的

import json

with open("4.json", encoding="utf8") as file:
    data = file.read()
    reader = json.loads(data.replace('\\"', '"').replace('\\\\"', '\\"'))
# while writing a json file use json.dump(file_object, reader)

结果:

{'Status': 'OK',
 'isSuccess': True,
 'Error': '',
 'Data': [{'STOK_KODU': 'REMA-50100',
           'BR1_PAYDA': 1,
           'MARKA': 'REMARK',
           'URETICI_KODU': '50100',
           'KDV_ORANI': 18.0,
           'STOK_DURUM': '3ARA',
           'IMG': 1,
           'STOK_TEXT': 'REM03@FAN MOTORU 24VANELI  7"@@ REMARK@ @@ XRFM1103@ '
                        '009 2985@ 2394009@ RF03',
           'ORAN1': 43.0,
           'ORAN2': 7.0,
           'ORAN3': 0.0,
           'ORAN4': 0.0,
           'ORAN5': 0.0,
           'ORAN6': 0.0,
           'ORAN7': 0.0,
           'SATIS1': 9067.9},
          {'STOK_KODU': 'REMA-03',
           'BR1_PAYDA': 1,
           'MARKA': 'REMARK',
           'URETICI_KODU': '503',
           'KDV_ORANI': 18.0,
           'STOK_DURUM': '3ARA',
           'IMG': 1,
           'STOK_TEXT': 'R103@MARS MOTORU 24V75 KW DELC955 D STR3@@ REMARK@ '
                        '@TYERA/BR23/ @ XRE0103@ @ 2313@ 50103',
           'ORAN1': 43.0,
           'ORAN2': 7.0,
           'ORAN3': 0.0,
           'ORAN4': 0.0,
           'ORAN5': 0.0,
           'ORAN6': 0.0,
           'ORAN7': 0.0,
           'SATIS1': 9067.9}]}
liwlm1x9

liwlm1x92#

使用

import json

with open("4.json", encoding="utf8") as file:
    txt = file.read()
    data = json.loads(txt)

相关问题