将子值设置为JSON中的URL

时间:2019-05-26 20:29:47

标签: python json firebase-realtime-database

我目前正在尝试将JSON文件中包含的信息(在本例中为新闻文章)发送给Firebase,格式如下:

{
"_type": "News",
"queryContext": {
    "originalQuery": "politics"
},
"readLink": "https://api.cognitive.microsoft.com/api/v7/news/search?q=politics",
"totalEstimatedMatches": 31800,
"value": [
    {
        "category": "Politics",
        "datePublished": "2019-05-26T19:53:00.0000000Z",
        "description": "The White House on Sunday brushed aside congressional Democrats\u2019 concerns about Atty. Gen. William Barr being handed extraordinary powers to declassify sensitive intelligence as part of a probe into the origins of the investigation into Russian efforts ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.AEA37BFFD9D0A9554FA9AEB2DFB82328&pid=News",
                "height": 393,
                "width": 700
            }
        },
        "name": "White House claims without proof that FBI has \u2018outrageous\u2019 corruption Barr will uncover",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_f6ee0777fa236afab60d094d1018c0b6&pid=news"
                    }
                },
                "name": "Los Angeles Times"
            }
        ],
        "url": "https://www.latimes.com/politics/la-na-pol-trump-fbi-corruption-20190526-story.html"
    },
    {
        "about": [
            {
                "name": "Academy of the Canyons",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/033a8c1d-1559-b2a4-43d7-a19935bc5a57"
            },
            {
                "name": "Hope",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/e35840e7-6a9f-44a3-919e-19ee36c21103"
            }
        ],
        "category": "Politics",
        "datePublished": "2019-05-26T19:40:00.0000000Z",
        "description": "Rep. Alexandria Ocasio-Cortez and other progressives criticized the New York Times on Sunday for writing that former White House aide Hope Hicks is facing an \"existential question\" over whether to comply with a congressional subpoena. Democrats on the ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.4BB68B63176AC7974EFE662AF950B9C3&pid=News",
                "height": 350,
                "width": 700
            }
        },
        "mentions": [
            {
                "name": "Academy of the Canyons"
            },
            {
                "name": "Hope"
            },
            {
                "name": "Business Insider"
            }
        ],
        "name": "Alexandria Ocasio-Cortez attacks former Trump aide Hope Hicks for 'considering participating in a coverup led by the President'",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_86ac6d5fd1859e509650fb5feef96bf7&pid=news"
                    }
                },
                "name": "Business Insider"
            }
        ],
        "url": "https://www.businessinsider.com/aoc-hope-hicks-attacks-former-trump-aide-trump-coverup-2019-5"
    },
    {
        "about": [
            {
                "name": "Joe Biden",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/cad484f9-be75-7a78-12dd-16233f823cd7"
            },
            {
                "name": "Campaign",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/b83d549f-1d62-a989-a518-4fdc76be546e"
            },
            {
                "name": "Exposure",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/fd725e08-73c1-d154-546c-7c07d8a59ff1"
            }
        ],
        "category": "Politics",
        "datePublished": "2019-05-26T20:03:00.0000000Z",
        "description": "Sen. Bernie Sanders (I-Vt.) plans to enjoy ice cream with New Hampshire voters to celebrate Memorial Day. He won\u2019t be far from former congressman John Delaney, another presidential candidate, who\u2019s in the midst of his 19th trip to the state and plans ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.D1E848B3E4536A1A8549F4102A3BEB82&pid=News",
                "height": 466,
                "width": 700
            }
        },
        "name": "Joe Biden\u2019s campaign of limited exposure: How long can he keep it up?",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_47a5d64fad0a365fb3b4d7af8c1361e7&pid=news"
                    }
                },
                "name": "The Washington Post"
            }
        ],
        "url": "https://www.washingtonpost.com/politics/joe-bidens-campaign-of-limited-exposure-how-long-can-he-keep-it-up/2019/05/26/dcc045e4-7e2e-11e9-8bb7-0fc796cf2ec0_story.html"
    },
    {
        "category": "Politics",
        "datePublished": "2019-05-26T20:01:00.0000000Z",
        "description": "Sen. Bernie Sanders, I-VT, has modestly misled or exaggerated some of his statements while campaigning for the 2020 Democratic nomination, according to a fact-checking report in the New York Times this weekend. Sanders, the longest-sitting Independent ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.0CA72069E1F8A492B7B9831C354BDB6E&pid=News",
                "height": 466,
                "width": 700
            }
        },
        "name": "New York Times Fact-Checks Bernie Sanders On Campaign Trail",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_9f6a911c9d042b01c6b85e9c12aa8647&pid=news"
                    }
                },
                "name": "International Business Times"
            }
        ],
        "url": "https://www.ibtimes.com/new-york-times-fact-checks-bernie-sanders-campaign-trail-2795072"
    },
    {
        "about": [
            {
                "name": "Raid",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/d2fb9c28-3dad-6f78-2902-d1e0b94762e1"
            }
        ],
        "category": "Politics",
        "datePublished": "2019-05-26T19:50:00.0000000Z",
        "description": "The San Francisco Police Officers Association on Saturday called for Police Chief Bill Scott to resign over the raid of a freelance journalist's home and office. \"It is time for Chief Scott to go.",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.58ABE32D6ABFD66302CF371380D901C2&pid=News",
                "height": 360,
                "width": 640
            }
        },
        "mentions": [
            {
                "name": "Raid"
            },
            {
                "name": "Union type"
            },
            {
                "name": "San Francisco Police Department"
            }
        ],
        "name": "San Francisco police union calls on chief to resign over journalist raid",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_fbfe507a57f598418fd179b0dec78111&pid=news"
                    }
                },
                "name": "The Hill"
            }
        ],
        "url": "https://thehill.com/homenews/state-watch/445615-san-francisco-police-union-calls-on-chief-to-resign-over-journalist-raid"
    },
    {
        "category": "Politics",
        "datePublished": "2019-05-26T19:12:00.0000000Z",
        "description": "This week on \"FOX News Sunday,\" Senate Judiciary Chairman Lindsey Graham said he strongly supported President Trump's decision to authorize the attorney general to declassify information related to the Russia investigation and he is disappointed that the ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.87AA2090AEE6F7A8E0305C058B6F37B7&pid=News",
                "height": 375,
                "width": 500
            }
        },
        "name": "Lindsey Graham: Not One Democrat Cares If FISA Court Was Defrauded By FBI And DOJ",
        "provider": [
            {
                "_type": "Organization",
                "name": "RealClearPolitics"
            }
        ],
        "url": "https://www.realclearpolitics.com/video/2019/05/26/lindsey_graham_not_one_democrat_cares_if_fisa_court_was_defrauded_by_fbi_and_doj.html"
    },
    {
        "category": "Politics",
        "datePublished": "2019-05-26T19:24:00.0000000Z",
        "description": "It seems nothing is too anti-Semitic for today\u2019s Democrats (\u201cDemocrats shoot down effort to rein in anti-Israel BDS movement,\u201d Web, May 23). The purpose of the Boycott, Divestment and Sanctions (BDS) movement is the elimination of Israel. BDS ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.805F4DD8F828E73C6D851B2797941ADB&pid=News",
                "height": 200,
                "width": 300
            }
        },
        "name": "LETTERS TO THE EDITOR: Jew-hater or weak, result same",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_104a9402f29a6a488f404306f122ddec&pid=news"
                    }
                },
                "name": "The Washington Times"
            }
        ],
        "url": "https://www.washingtontimes.com/news/2019/may/26/letters-to-the-editor-jew-hater-or-weak-result-sam/"
    },
    {
        "about": [
            {
                "name": "Nancy Pelosi",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/707086df-b7b0-a823-28d8-79914cbfbd4e"
            },
            {
                "name": "Donald Trump",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/1a466af2-ed23-25bd-794d-1ca925e4681b"
            }
        ],
        "category": "Politics",
        "datePublished": "2019-05-26T19:40:00.0000000Z",
        "description": "A Republican lawmaker said Sunday that he is concerned about President Trump\u2019s sharing of a heavily edited video that spliced together several verbal pauses by House Speaker Nancy Pelosi (D-Calif.). Rep. Will Hurd (R-Tex.) said that by sharing the video ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.2A6AB9FEB5A2F6110247FD7A8962F210&pid=News",
                "height": 466,
                "width": 700
            }
        },
        "name": "Republican lawmaker criticizes Trump for sharing heavily edited video of Pelosi",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_47a5d64fad0a365fb3b4d7af8c1361e7&pid=news"
                    }
                },
                "name": "The Washington Post"
            }
        ],
        "url": "https://www.washingtonpost.com/politics/republican-lawmaker-criticizes-trump-for-sharing-heavily-edited-video-of-pelosi/2019/05/26/6e8b310e-7fde-11e9-933d-7501070ee669_story.html"
    },
    {
        "category": "Politics",
        "datePublished": "2019-05-26T19:13:00.0000000Z",
        "description": "Harvard Law Professor Laurence Tribe told MSNBC's Joy Ann Reid Saturday morning on \"AM Joy\" that House Speaker Nancy Pelosi is right when she calls for an \"intervention\" against President Trump, but \"the most important kind of intervention is not a little ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.80B21F562977304C931AFB246B84696A&pid=News",
                "height": 375,
                "width": 500
            }
        },
        "name": "Laurence Tribe to Nancy Pelosi: The Most Important Kind Of \"Intervention\" Is An Impeachment Inquiry",
        "provider": [
            {
                "_type": "Organization",
                "name": "RealClearPolitics"
            }
        ],
        "url": "https://www.realclearpolitics.com/video/2019/05/26/laurence_tribe_to_nancy_pelosi_the_most_important_kind_of_intervention_is_an_impeachment_inquiry.html"
    },
    {
        "about": [
            {
                "name": "Lindsey Graham",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/9ba65851-7a4a-18c2-c979-0daf5d69d46b"
            },
            {
                "name": "Chris Wallace",
                "readLink": "https://api.cognitive.microsoft.com/api/v7/entities/47e2deef-f6db-38b8-64fc-4ede147de399"
            }
        ],
        "category": "Politics",
        "datePublished": "2019-05-26T19:45:00.0000000Z",
        "description": "Trump's Point Man on China Trade Still Trying to Close the Deal Fox News Host Chris Wallace on Sunday challenged Sen. Lindsey Graham over the South Carolina Republican's past comments on ignoring subpoenas being grounds for impeachment. \"You call all of ...",
        "image": {
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.554F80686BD1C7DEF8D112F3FB3384B6&pid=News",
                "height": 315,
                "width": 600
            }
        },
        "mentions": [
            {
                "name": "Lindsey Graham"
            },
            {
                "name": "Chris Wallace"
            },
            {
                "name": "Fox News Channel"
            }
        ],
        "name": "Fox's Chris Wallace challenges Graham over past comments on ignoring subpoenas",
        "provider": [
            {
                "_type": "Organization",
                "image": {
                    "thumbnail": {
                        "contentUrl": "https://www.bing.com/th?id=AR_fbfe507a57f598418fd179b0dec78111&pid=news"
                    }
                },
                "name": "The Hill on MSN.com"
            }
        ],
        "url": "https://www.msn.com/en-us/news/politics/foxs-chris-wallace-challenges-graham-over-past-comments-on-ignoring-subpoenas/ar-AABWiQt"
    }
]

虽然我已经能够将JSON数据发送到Firebase,但是子值的编号范围是0-9(我的JSON有10条新闻文章)。 Here's the JSON output in Firebase

JSON文件每15分钟更新一次最新消息,以替换此JSON中的内容(仅包含10条最新文章)。每当我将最新的JSON发送到Firebase时,当我想添加更多文章时,它将替换以前的0-9值。

为了防止替换这些值,我想将子值设置为JSON中该文章的“ url”。而不是显示为0、1、2、3等,而是将其列为相应的文章网址

这是我的将JSON上传到Firebase的代码:

with open ('/var/www/mywebsite.com/html/private/scripts/data.json') as data_file:
    data=json.load(data_file)
    jsondata = json.dumps('/var/www/mywebsite.com/html/private/scripts/data.json')

cred = credentials.Certificate('/var/www/mywebsite.com/html/private/scripts/firebase-adminsdk.json')
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://myapp.firebaseio.com/'
})

ref = db.reference('Feed')
ref.set(data)

我将如何去做?我是Python和Firebase的新手,请原谅我对某些问题的措辞。

0 个答案:

没有答案