Как разобрать JSON в django каркас отдыха - PullRequest
0 голосов
/ 16 января 2020

Я нуб ie в DRF, поэтому, пожалуйста, прости меня, если это глупо, но это мой JSON файл:

"articles": [
        {
            "source": {
                "id": "techcrunch",
                "name": "TechCrunch"
            },
            "author": "Jonathan Shieber",
            "title": "All tulips must wilt",
            "description": "It’s a bad day in the world of cryptocurrency. After recovering some during the summer, the value of bitcoin and other cryptocurrencies are sharply down over the last several weeks. Looking back a month, bitcoin was worth around $8,500 a coin. Today it’s wort…",
            "url": "http://techcrunch.com/2019/12/18/all-tulips-must-wilt/",
            "urlToImage": "https://techcrunch.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-18-at-9.39.21-AM.png?w=669",
            "publishedAt": "2019-12-18T15:17:17Z",
            "content": "It’s a bad day in the world of cryptocurrency. After recovering some during the summer, the value of bitcoin and other cryptocurrencies are sharply down over the last several weeks. Looking back a month, bitcoin was worth around $8,500 a coin. Today it’s wort… [+673 chars]"
        },
        {
            "source": {
                "id": "mashable",
                "name": "Mashable"
            },
            "author": "Stan Schroeder",
            "title": "Bitcoin whale moves $1.1 billion in bitcoins for an $80 fee",
            "description": "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing…",
            "url": "https://mashable.com/article/bitcoin-1-1-billion-transaction/",
            "urlToImage": "https://mondrian.mashable.com/2020%252F01%252F15%252F38%252Fd26e834787934c56a33fdeb39faa0be8.84e34.jpg%252F1200x630.jpg?signature=IHj6xz7nTFxvmjn6XOvUiHKJCIM=",
            "publishedAt": "2020-01-15T09:10:59Z",
            "content": "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing… [+1589 chars]"
        },

    ]

И это мои views.py

@api_view(['GET','POST'])
def index(request):
    if(request.method == 'POST'):
        print(request.data)
        return Response({"message": "Got some data!"})
    else:
        message = 'This is a testing message'
        return Response(data = message, status=status.HTTP_200_OK)

Я получаю здесь ошибку:

{
    "detail": "JSON parse error - Extra data: line 1 column 11 (char 10)"
}

Пожалуйста, помогите мне

1 Ответ

0 голосов
/ 16 января 2020

вместо использования request.data попробуйте

req_body = json.loads(request.body.decode('utf-8'))

, а затем вы можете использовать req_body как dict в python

...