如何从json一行字典中获取字典

时间:2019-11-15 19:11:20

标签: python json python-3.x dictionary

我有一个包含字典的json文件,我想获取该字典。我尝试了json.load(),但出现此错误消息:'json.decoder.JSONDecodeError:期望值:第1行第1列(char 0)' 我在许多网站上都看到了这种方法。 我的json文件看起来像这样:(这只是一个例子,因为我不想显示我的代码)

{"A": 4, "B": 12}

我的python看起来像这样:

import json

with open("file.json", "r+") as f:
    d = json.load(f)

能帮我吗? (我正在使用Python 3.7)

3 个答案:

答案 0 :(得分:0)

您需要在值两边加上引号,而不是

WITH Calendar (Start_Date, End_Date)
AS (SELECT CAST('2019-06-01' AS DATETIME2) AS [Start_Date],
           CAST('2019-11-30' AS DATETIME2) AS [End_Date])
SELECT Start_Date,
       End_Date,
       (
           SELECT COUNT(DISTINCT ucs.ResourceID)
           FROM v_Update_ComplianceStatus ucs
               JOIN v_UpdateInfo ui
                   ON ui.CI_ID = ucs.CI_ID
                      AND ucs.Status = 2
                      AND ui.IsSuperseded = 0
               JOIN v_CICategoryInfo_All cica
                   JOIN v_CategoryInfo ci
                       ON cica.CategoryInstanceID = ci.CategoryInstanceID
                          AND cica.CategoryInstanceName IN ( @Category )
                   ON ucs.CI_ID = cica.CI_ID
           WHERE ucs.ResourceID IN
                 (
                     SELECT rs.ResourceID
                     FROM v_r_system rs
                         JOIN V_FullCollectionMembership AS FCM
                             ON rs.ResourceID = FCM.ResourceID
                                AND FCM.CollectionID = @CollectionID
                         JOIN v_UpdateScanStatus uss
                             ON uss.ResourceID = rs.ResourceID
                     WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 6.1%'
                           AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                           AND rs.Obsolete0 = 0
                 )
                 AND ui.DateRevised
                 BETWEEN cal.Start_Date AND cal.End_Date
       ) [Windows 7],
       (
           SELECT COUNT(rs.ResourceID)
           FROM v_r_system rs
               JOIN V_FullCollectionMembership AS FCM
                   ON rs.ResourceID = FCM.ResourceID
                      AND FCM.CollectionID = @CollectionID
               JOIN v_UpdateScanStatus uss
                   ON uss.ResourceID = rs.ResourceID
           WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 6.3%'
                 AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                 AND rs.Obsolete0 = 0
       ) [Windows 8.1 Total],
       (
           SELECT COUNT(DISTINCT ucs.ResourceID)
           FROM v_Update_ComplianceStatus ucs
               JOIN v_UpdateInfo ui
                   ON ui.CI_ID = ucs.CI_ID
                      AND ucs.Status = 2
                      AND ui.IsSuperseded = 0
               JOIN v_CICategoryInfo_All cica
                   JOIN v_CategoryInfo ci
                       ON cica.CategoryInstanceID = ci.CategoryInstanceID
                          AND cica.CategoryInstanceName IN ( @Category )
                   ON ucs.CI_ID = cica.CI_ID
           WHERE ucs.ResourceID IN
                 (
                     SELECT rs.ResourceID
                     FROM v_r_system rs
                         JOIN V_FullCollectionMembership AS FCM
                             ON rs.ResourceID = FCM.ResourceID
                                AND FCM.CollectionID = @CollectionID
                         JOIN v_UpdateScanStatus uss
                             ON uss.ResourceID = rs.ResourceID
                     WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 10%'
                           AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                           AND rs.Obsolete0 = 0
                 )
                 AND ui.DateRevised
                 BETWEEN cal.Start_Date AND cal.End_Date
       ) [Windows 10],
       (
           SELECT COUNT(rs.ResourceID)
           FROM v_r_system rs
               JOIN V_FullCollectionMembership AS FCM
                   ON rs.ResourceID = FCM.ResourceID
                      AND FCM.CollectionID = @CollectionID
               JOIN v_UpdateScanStatus uss
                   ON uss.ResourceID = rs.ResourceID
           WHERE rs.Operating_System_Name_and0 LIKE '%Workstation 10%'
                 AND DATEDIFF("d", uss.LastScanTime, GETDATE()) <= @DaysScanned
                 AND rs.Obsolete0 = 0
       ) [Windows 10 Total]
FROM Calendar cal;

它必须是

{"A": 4, "B": 12}

答案 1 :(得分:0)

您正在将文件读入f并尝试加载j(甚至未指定)

尝试以下代码:

with open("test.json", "r+") as j:
    d = json.load(j)

print(d)
{'A': 4, 'B': 12}

答案 2 :(得分:0)

检查行尾。 它们必须为LF(以\n结尾)。有时这可能会引起问题。