【问题标题】:raise JSONDecodeError("Expecting value", s, err.value) from None Python从无 Python 引发 JSONDecodeError("Expecting value", s, err.value)
【发布时间】:2021-09-29 14:55:05
【问题描述】:

我有时会遇到这个问题。但总能解决。这下不可能找到解决办法了。同一主题有很多帖子,但在我的情况下没有解决方案。 我正在尝试从我的 api 获取数据。它有时可以工作(使用没有无值的数据)并且使用包含无值的数据,它不起作用。我认为问题在于无值。我有这个错误

Exception Type: JSONDecodeError
Exception Value:    
Expecting value: line 1 column 75 (char 74)

感谢任何帮助。

@api_view(['GET'])
def edges_of_a_network(request, pk):
    try:
        roadnetwork = RoadNetwork.objects.get(pk=pk)
    except RoadNetwork.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    else:
        edges = Edge.objects.filter(network=roadnetwork)
        edges = str(list(edges.values("edge_id", "name",
            "length", "speed", "lanes"))).replace("'",'"')
        edges = json.loads(edges)
        edges = json.dumps(edges, indent=2)

    if request.method == 'GET':
        return HttpResponse(edges, content_type="application/json")

截图

【问题讨论】:

    标签: python json django django-rest-framework jsondecoder


    【解决方案1】:

    我终于找到了解决问题的方法。我添加了这一行 edges = edges.replace('None', "null")。我将None values 替换为null values

    @api_view(['GET'])
    def edges_of_a_network(request, pk):
        try:
            roadnetwork = RoadNetwork.objects.get(pk=pk)
        except RoadNetwork.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
    
        else:
            edges = Edge.objects.filter(network=roadnetwork)
            edges = str(list(edges.values("edge_id", "name",
                "length", "speed", "lanes"))).replace("'",'"')
            edges = edges.replace('None', "null")
            edges = json.loads(edges)
            edges = json.dumps(edges, indent=2)
    
        if request.method == 'GET':
            return HttpResponse(edges, content_type="application/json")
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2019-09-18
      • 2020-04-19
      • 2022-06-11
      • 2017-05-11
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多