gpt4 book ai didi

python - 如何让 json.loads(json-string) 返回字典而不是字符串或字符串列表?

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:58 24 4
gpt4 key购买 nike

我正在使用 python-requests 来处理 elasticsearch 索引。特别是我没有从 requests.json() 得到我想要的东西(在标准库中包装 json.loads() 函数)。

我正在努力处理 json() 方法返回的结果。它似乎正在返回一个字符串列表。我想要一个可以循环访问每个成员属性的 dict 对象,即......

r = requests.get(url)
results = r.json() //same as results = json.loads(json string)
print(results)

for result in results:
print(result['source'])

这是我手动使用 curl 执行搜索时 Elasticsearch 返回的内容(这是 r.json() 调用的输入):

{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":18,"max_score":1.0,"hits":[{"_index":"feed","_type":"article","_id":"Bq2_NxWIRZS5mfX_jxv5YQ","_score":1.0, "_so    urce" : {"source": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "link": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "description    ": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Food/Salmon-Raw.jpg\" /></td><td valign=\"top\">Equally as menacing as the potential release of genetically-modified (GM) salmon     into the wild, factory fish farms are a modern scourge responsible for killing off droves of native fish species all around the world. And unless immediate action is taken to address this...</td></tr></t    able>"], "title": ["Fish farming killing off native species; boycott farmed salmon before it's too late"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 193}},{"_index":"feed","_type":    "article","_id":"jxfFD6V-Qwacc4KYNfChcg","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "link": ["http://www.naturalnew    s.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Exercise/Running-Shoes-Fitness-Legs-Calves-Mus    cles.jpg\" /></td><td valign=\"top\">Building killer calves is more complicated than people think. Most exercisers do a bunch of standing calf exercises like standing calf raises, sled hack calf raises,     donkey raises, and calf presses to bulk up not realizing that their entire workout is only targeting...</td></tr></table>"], "title": ["Calf exercises: Sit down to build yourself up"], "published": ["Wed    , 5 Jun 2013 00:00:00 CST"], "medoozie_score": 228}},{"_index":"feed","_type":"article","_id":"RBV1P_lXStKAHeaNi-q9Bg","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040626_Hopewood_ch    ildren_natural_living_organic_foods.html"], "link": ["http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html"], "description": ["<table><tr><td><img src=\"http://www.natura    lnews.com/gallery/300X250/Children/Kids-Children-Play-Outside-Happy-Exercise.jpg\" /></td><td valign=\"top\">In 1942, the founder of the Natural Health Society of Australia, Leslie Owen Bailey, began a s    ocial experiment: He chose to adopt 85 children whose parents were unable to provide for them. The children's residence became the sprawling Hopewood House in New South Wales...</td></tr></table>"], "tit    le": ["Honoring nature: The lesson of the Hopewood children"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 187}},{"_index":"feed","_type":"article","_id":"uP6G2_leSl2-gUMyUie3eA","_s    core":1.0, "_source" : {"source": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_groups_Obama_Administration.html"], "link": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_gro    ups_Obama_Administration.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Money/IRS-Building-Revenue-Taxes-Money.jpg\" /></td><td valign=\"top\">Officials wit    h the Internal Revenue Service (IRS) have admitted that the agency inappropriately targeted conservative groups seeking legal tax-exemption status in the years and months leading up to the 2012 election.    &#13;&#10;&#13;&#10;Those same officials have apologized for the targeting...</td></tr></table>"], "title": ["Claim: IRS still targeting conservative groups, even after promising the intimidation already     stopped"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 342}},{"_index":"feed","_type":"article","_id":"yhcqfmKQRwSVhR1vUIy0vg","_score":1.0, "_source" : {"source": ["http://www.natu    ralnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "link": ["http://www.naturalnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "description": ["<table><tr><td><img s    rc=\"http://www.naturalnews.com/gallery/300X250/Women/Party-Girls-Fun.jpg\" /></td><td valign=\"top\">Are people in Austin so hungry they are eating bugs?&#13;&#10;&#13;&#10;\"Well, it does help if you c    ome to the party having skipped lunch,\" says Marjory Wildcraft, the host and creator 6th Annual Bug Eating Festival which will be held on Saturday June 29, 2013 at Zilker Park in Austin...</td></tr></ta    ble>"], "title": ["6th Annual Bug Eating Festival"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 252}},{"_index":"feed","_type":"article","_id":"Grh9KUDVQvSs7oRUjzOG9A","_score":1.0,     "_source" : {"source": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "link": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "description"    : ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Food/Brown-Eggs-On-White.jpg\" /></td><td valign=\"top\">Considering that eggs contain all the nutrients needed to develop healthy     chicks, it is unsurprising that studies into their numerous health benefits for humans abound. Eating more eggs has, for instance, been repeatedly linked to reduced LDL cholesterol, a lowered risk...</t    d></tr></table>"], "title": ["Peptide found in egg whites can lower blood pressure, say researchers"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 262}},{"_index":"feed","_type":"art    icle","_id":"6OfIM7G9Rke-TD3N6bI4Ow","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html"], "link": ["http://www.naturalnews.com/040629_obe    sity_vitamin_D_deficiency_exercise.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Men/Big-Fat-Hairy-Obese-Belly.jpg\" /></td><td valign=\"top\">Confirming w    hat health professionals have long suspected, a new study published in the journal PLOS Medicine provides some of the strongest evidence yet that obesity is indeed a cause of vitamin D deficiency.&#13;&#    10;&#13;&#10;The large study was a collaborative effort between U.S...</td></tr></table>"], "title": ["Obesity causes vitamin-D deficiency: Research"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "med    oozie_score": 313}},{"_index":"feed","_type":"article","_id":"osvu1IlkSwa9yL566YoQuQ","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_    press.html"], "link": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_press.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/C    oncepts/Extra-Liar-Newspaper-Headline.jpg\" /></td><td valign=\"top\">Terrorism, and the threat of new attacks, dominated U.S. domestic policy and national security strategy throughout the Bush Administr    ation, but my, how times have changed: Despite the recent terrorist bombings of the Boston Marathon April 19, President Barack Obama appears...</td></tr></table>"], "title": ["Judge Napolitano: Obama mor    e threatened by journalism than terrorism"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 34}},{"_index":"feed","_type":"article","_id":"0XfVTXImTWGwQ1Ko13Q9hg","_score":1.0, "_source    " : {"source": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "link": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "description": ["<table><tr>    <td><img src=\"http://www.naturalnews.com/gallery/300X250/Cancer/Poor-Women-Breast-Cancer.jpg\" /></td><td valign=\"top\">For reasons that are still largely unclear, women are much more likely than men t    o develop some form of mental illness during their lives, whether it be depression, schizophrenia, or simple anxiety, according to a new study. Based on an analysis of 12 large-scale epidemiological...</    td></tr></table>"], "title": ["Women are 40 percent more likely to develop mental illness than men, study concludes"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 434}},{"_index":"fe    ed","_type":"article","_id":"8L699AbUTtmlrnlSJlmzhw","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040624_cancer_industry_documentary_lies.html"], "link": ["http://www.naturalnews.com    /040624_cancer_industry_documentary_lies.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300x250/Programs/Cut-Poison-Burn.jpg\" /></td><td valign=\"top\">The stunnin    g, jaw-dropping and tearfully devastating true story of the conventional cancer industry is unleashed in a new documentary called \"Cut, Poison, Burn.\" While celebrities like Michael Douglas are grabbin    g headlines by promoting the dangerous myths and false...</td></tr></table>"], "title": ["Profiteering cancer industry ripped wide open by explosive documentary 'Cut, Poison, Burn' - watch preview here"]    , "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 187}}]}}

这是我将 request.json() 返回的结果写入 txt 文件时得到的结果:

'{"source": ["http://www.naturalnews.com/040640_front_groups_food_industry_processed.html"], "link": ["http://www.naturalnews.com/040640_front_groups_food_industry_processed.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Logos/Pepsi-Logo.jpg\\" /></td><td valign=\\"top\\">Some say it\'s the best public relations that money can buy, because when you can\'t refute the truth about an issue, the next-best thing to do is stack the deck in your favor.&#13;&#10;&#13;&#10;The third edition of a report released last month by the International Food Information Council...</td></tr></table>"], "title": ["Food industry front groups exposed in new report: Monsanto, Coke, Pepsi and more"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 350}'
'{"source": ["http://www.naturalnews.com/040639_Obamacare_insurance_premiums_individual_mandate.html"], "link": ["http://www.naturalnews.com/040639_Obamacare_insurance_premiums_individual_mandate.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300x250/celebrities/Barack-Obama-Portrait.jpg\\" /></td><td valign=\\"top\\">The scramble is on to bring the logistical framework for Obamacare into full fruition before its inaugural arrival in 2014. But as legislators and program architects dig down into the nitty-gritty of what the \\"tax\\" will actually entail in practicality, it is becoming...</td></tr></table>"], "title": ["Obamacare will collapse under the weight of rising premiums"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 121}'
'{"source": ["http://www.naturalnews.com/040638_Garcinia_cambogia_weight_loss_obestity.html"], "link": ["http://www.naturalnews.com/040638_Garcinia_cambogia_weight_loss_obestity.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Men/Belly-Fat.jpg\\" /></td><td valign=\\"top\\">With celeb Dr. Oz advocating its use, Garcinia Cambogia (also known as Malabar tamarind, gambooge and brindleberry), has gained notoriety as a fast-acting and cheap fat buster that both suppresses appetite and prevents fat from being made. For generations, this fruit...</td></tr></table>"], "title": ["Does Garcinia cambogia extract really help with weight loss?"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 120}'
'{"source": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "link": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Cancer/Poor-Women-Breast-Cancer.jpg\\" /></td><td valign=\\"top\\">For reasons that are still largely unclear, women are much more likely than men to develop some form of mental illness during their lives, whether it be depression, schizophrenia, or simple anxiety, according to a new study. Based on an analysis of 12 large-scale epidemiological...</td></tr></table>"], "title": ["Women are 40 percent more likely to develop mental illness than men, study concludes"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 468}'
'{"source": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_groups_Obama_Administration.html"], "link": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_groups_Obama_Administration.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Money/IRS-Building-Revenue-Taxes-Money.jpg\\" /></td><td valign=\\"top\\">Officials with the Internal Revenue Service (IRS) have admitted that the agency inappropriately targeted conservative groups seeking legal tax-exemption status in the years and months leading up to the 2012 election.&#13;&#10;&#13;&#10;Those same officials have apologized for the targeting...</td></tr></table>"], "title": ["Claim: IRS still targeting conservative groups, even after promising the intimidation already stopped"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 12}'
'{"source": ["http://www.naturalnews.com/040635_cooling_foods_summer_eating_whole.html"], "link": ["http://www.naturalnews.com/040635_cooling_foods_summer_eating_whole.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Women/Woman-Summersault-Grass-Fitness-Play.jpg\\" /></td><td valign=\\"top\\">As we enter into the dog days of summer, it\'s easy to keep cool with specific foods and herbs under our belt. Using the wisdom of Traditional Chinese Medicine (TCM), we can remain balanced, calm and energetic as the mercury rises. Particular suggestions border on simple...</td></tr></table>"], "title": ["Stay cool this summer with natural medicine and herbal remedies"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 495}'
'{"source": ["http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html"], "link": ["http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Concepts/Man-Hat-Brain-Concept.jpg\\" /></td><td valign=\\"top\\">President Obama gave psychiatrists and pharmaceutical companies the keys to the kingdom during his keynote speech at the White House Conference on Mental Health this week.&#13;&#10;&#13;&#10;Celebrated as a hero to the shamed and downtrodden, Obama played the part well as he subtly...</td></tr></table>"], "title": ["Obama keynote speech deals death blow to mental health, delivers millions into the hands of big pharma"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 280}'
'{"source": ["http://www.naturalnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "link": ["http://www.naturalnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Women/Party-Girls-Fun.jpg\\" /></td><td valign=\\"top\\">Are people in Austin so hungry they are eating bugs?&#13;&#10;&#13;&#10;\\"Well, it does help if you come to the party having skipped lunch,\\" says Marjory Wildcraft, the host and creator 6th Annual Bug Eating Festival which will be held on Saturday June 29, 2013 at Zilker Park in Austin...</td></tr></table>"], "title": ["6th Annual Bug Eating Festival"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 191}'
'{"source": ["http://www.naturalnews.com/040632_C-sections_obesity_childbirth.html"], "link": ["http://www.naturalnews.com/040632_C-sections_obesity_childbirth.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Medical/Woman-Giving-Birth-Delivering-Baby-Nurses-Doctors-Hospital.jpg\\" /></td><td valign=\\"top\\">New research out of the U.K. sheds light on yet another possible cause of widespread childhood obesity. Based on an analysis involving more than 10,000 British babies, roughly 9 percent of whom were born via C-section, researchers found that babies delivered the alternate...</td></tr></table>"], "title": ["C-sections lead to obese kids, study finds"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 64}'
'{"source": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "link": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Food/Salmon-Raw.jpg\\" /></td><td valign=\\"top\\">Equally as menacing as the potential release of genetically-modified (GM) salmon into the wild, factory fish farms are a modern scourge responsible for killing off droves of native fish species all around the world. And unless immediate action is taken to address this...</td></tr></table>"], "title": ["Fish farming killing off native species; boycott farmed salmon before it\'s too late"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 281}'
'{"source": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "link": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Food/Brown-Eggs-On-White.jpg\\" /></td><td valign=\\"top\\">Considering that eggs contain all the nutrients needed to develop healthy chicks, it is unsurprising that studies into their numerous health benefits for humans abound. Eating more eggs has, for instance, been repeatedly linked to reduced LDL cholesterol, a lowered risk...</td></tr></table>"], "title": ["Peptide found in egg whites can lower blood pressure, say researchers"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 118}'
'{"source": ["http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html"], "link": ["http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Men/Big-Fat-Hairy-Obese-Belly.jpg\\" /></td><td valign=\\"top\\">Confirming what health professionals have long suspected, a new study published in the journal PLOS Medicine provides some of the strongest evidence yet that obesity is indeed a cause of vitamin D deficiency.&#13;&#10;&#13;&#10;The large study was a collaborative effort between U.S...</td></tr></table>"], "title": ["Obesity causes vitamin-D deficiency: Research"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 395}'
'{"source": ["http://www.naturalnews.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "link": ["http://www.naturalnews.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Exercise/Running-Shoes-Fitness-Legs-Calves-Muscles.jpg\\" /></td><td valign=\\"top\\">Building killer calves is more complicated than people think. Most exercisers do a bunch of standing calf exercises like standing calf raises, sled hack calf raises, donkey raises, and calf presses to bulk up not realizing that their entire workout is only targeting...</td></tr></table>"], "title": ["Calf exercises: Sit down to build yourself up"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 469}'
'{"source": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_press.html"], "link": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_press.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Concepts/Extra-Liar-Newspaper-Headline.jpg\\" /></td><td valign=\\"top\\">Terrorism, and the threat of new attacks, dominated U.S. domestic policy and national security strategy throughout the Bush Administration, but my, how times have changed: Despite the recent terrorist bombings of the Boston Marathon April 19, President Barack Obama appears...</td></tr></table>"], "title": ["Judge Napolitano: Obama more threatened by journalism than terrorism"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 96}'
'{"source": ["http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html"], "link": ["http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Children/Kids-Children-Play-Outside-Happy-Exercise.jpg\\" /></td><td valign=\\"top\\">In 1942, the founder of the Natural Health Society of Australia, Leslie Owen Bailey, began a social experiment: He chose to adopt 85 children whose parents were unable to provide for them. The children\'s residence became the sprawling Hopewood House in New South Wales...</td></tr></table>"], "title": ["Honoring nature: The lesson of the Hopewood children"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 445}'
'{"source": ["http://www.naturalnews.com/040625_lawsuit_Monsanto_genetic_pollution.html"], "link": ["http://www.naturalnews.com/040625_lawsuit_Monsanto_genetic_pollution.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Farming/Man-Farmer-Wheat-Crop-Field-Clouds-Sky.jpg\\" /></td><td valign=\\"top\\">The next wave of farmer backlash against Monsanto has just been unleashed by Ernest Barnes, a wheat farmer in Morton County, Kansas. He filed suit this week in the U.S. District Court in Wichita, Kansas, alleging that Monsanto\'s genetic pollution has financially damaged...</td></tr></table>"], "title": ["It\'s on! Farmers begin suing Monsanto over genetic pollution of wheat crops"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 311}'
'{"source": ["http://www.naturalnews.com/040624_cancer_industry_documentary_lies.html"], "link": ["http://www.naturalnews.com/040624_cancer_industry_documentary_lies.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300x250/Programs/Cut-Poison-Burn.jpg\\" /></td><td valign=\\"top\\">The stunning, jaw-dropping and tearfully devastating true story of the conventional cancer industry is unleashed in a new documentary called \\"Cut, Poison, Burn.\\" While celebrities like Michael Douglas are grabbing headlines by promoting the dangerous myths and false...</td></tr></table>"], "title": ["Profiteering cancer industry ripped wide open by explosive documentary \'Cut, Poison, Burn\' - watch preview here"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 216}'
'{"source": ["http://www.naturalnews.com/040623_Morninglane_Dairy_Vernon_Hershberger_food_freedom.html"], "link": ["http://www.naturalnews.com/040623_Morninglane_Dairy_Vernon_Hershberger_food_freedom.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Farming/Farmer-Crops.jpg\\" /></td><td valign=\\"top\\">There is a war being waged against real food - no, not the heavily-processed, chemical-laden garbage that fills the aisles of most major supermarkets today, but actual wholesome food grown on clean, family-scale farms across the U.S. And the upcoming documentary Let...</td></tr></table>"], "title": ["\'Let Them Eat Grass\' documentary to expose massive government tyranny against small-scale farmers producing real food"], "published": ["Tue, 4 Jun 2013 00:00:00 CST"], "medoozie_score": 211}'
{u'hits': {u'hits': [{u'_score': 0.32461247, u'_type': u'article', u'_id': u'yBqM5NSoTDCAeXo2YRO1hQ', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Concepts/Man-Hat-Brain-Concept.jpg" /></td><td valign="top">President Obama gave psychiatrists and pharmaceutical companies the keys to the kingdom during his keynote speech at the White House Conference on Mental Health this week.&#13;&#10;&#13;&#10;Celebrated as a hero to the shamed and downtrodden, Obama played the part well as he subtly...</td></tr></table>'], u'title': [u'Obama keynote speech deals death blow to mental health, delivers millions into the hands of big pharma'], u'source': [u'http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html'], u'link': [u'http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html'], u'medoozie_score': 93, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}, {u'_score': 0.16524655, u'_type': u'article', u'_id': u'Grh9KUDVQvSs7oRUjzOG9A', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Food/Brown-Eggs-On-White.jpg" /></td><td valign="top">Considering that eggs contain all the nutrients needed to develop healthy chicks, it is unsurprising that studies into their numerous health benefits for humans abound. Eating more eggs has, for instance, been repeatedly linked to reduced LDL cholesterol, a lowered risk...</td></tr></table>'], u'title': [u'Peptide found in egg whites can lower blood pressure, say researchers'], u'source': [u'http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html'], u'link': [u'http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html'], u'medoozie_score': 262, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}, {u'_score': 0.16524655, u'_type': u'article', u'_id': u'6OfIM7G9Rke-TD3N6bI4Ow', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Men/Big-Fat-Hairy-Obese-Belly.jpg" /></td><td valign="top">Confirming what health professionals have long suspected, a new study published in the journal PLOS Medicine provides some of the strongest evidence yet that obesity is indeed a cause of vitamin D deficiency.&#13;&#10;&#13;&#10;The large study was a collaborative effort between U.S...</td></tr></table>'], u'title': [u'Obesity causes vitamin-D deficiency: Research'], u'source': [u'http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html'], u'link': [u'http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html'], u'medoozie_score': 313, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}, {u'_score': 0.15372275, u'_type': u'article', u'_id': u'RBV1P_lXStKAHeaNi-q9Bg', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Children/Kids-Children-Play-Outside-Happy-Exercise.jpg" /></td><td valign="top">In 1942, the founder of the Natural Health Society of Australia, Leslie Owen Bailey, began a social experiment: He chose to adopt 85 children whose parents were unable to provide for them. The children\'s residence became the sprawling Hopewood House in New South Wales...</td></tr></table>'], u'title': [u'Honoring nature: The lesson of the Hopewood children'], u'source': [u'http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html'], u'link': [u'http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html'], u'medoozie_score': 187, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}], u'total': 4, u'max_score': 0.32461247}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 66, u'timed_out': False}

如何让 json.loads(json-string) 返回字典而不是字符串或字符串列表?如果这不可能,那么将列表中的每个字符串转换为字典的好方法是什么?

事实证明 r.json() 函数从未被破坏。我只是不知道如何遍历结果。

下面是我如何修改我的代码以便我可以遍历它...

r = requests.get('http://localhost:9200/feed/_search?q=' + query)
results = r.json()

for result in results['hits']['hits']:
print(result['_source']['title'])

这是我正在解码的 json 的较小示例...

http://pastebin.com/rpCDyL6F

最佳答案

事实证明 r.json() 函数从未被破坏。我只是不知道如何遍历结果。

下面是我如何修改我的代码以便我可以遍历它...

r = requests.get('http://localhost:9200/feed/_search?q=' + query)
results = r.json()

for result in results['hits']['hits']:
print(result['_source']['title'])

这是我正在解码的 json 的较小示例...

http://pastebin.com/rpCDyL6F

关于python - 如何让 json.loads(json-string) 返回字典而不是字符串或字符串列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16941601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com