如何使json.loads(json字符串)返回一个字典而不是一个字符串或字符串列表?

4
我正在使用python-requests与elasticsearch索引交互。特别地,我没有从requests.json()中得到我想要的结果(它包装了标准库中的json.loads()函数)。
我正在苦恼于json()方法返回的结果。似乎它返回了一个字符串列表。我想要一个字典对象,以便可以循环访问每个成员的属性,例如...
r = requests.get(url)
results = r.json() //same as results = json.loads(json string)
print(results)

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

这是当我手动使用curl进行搜索时,elastic search返回的结果(这是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


(注意:此处为直译,请根据上下文理解)

你的问题有点混乱,测试集也有点大。不如构建一个最小的不工作示例怎么样? - kirelagin
你提供的文本文件看起来像是一个字典。你是说它被保存在字符串值中吗? - SuperFamousGuy
@kirelagin,我不确定如何制作一个更小的示例。这只是因为搜索返回了大约20个结果,所以它很大。 - guyja
@kirelagin添加了较小的JSON结果示例。谢谢。 - guyja
@user2455857 很高兴听到这个消息!这比尝试评估返回的东西要干净得多。无论如何,什么样的疯子会建议呢? :) - SuperFamousGuy
显示剩余3条评论
2个回答

3
原来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

(注意:该段内容已经是中文,无需翻译)

-1

很遗憾,我对标准的json.loads函数及其提供的内容并不太熟悉,所以请注意这可能不是最好的方法。话虽如此,在JSON解析方面,将字符串转换为Python对象的最佳方法通常是使用ast.literal_eval。有一篇关于它的使用和危险性的好文章可以在这里找到。还有一篇关于eval与ast.literal_eval之间差异的好文章可以在这里找到。

请记住,为了将字符串评估为Python对象,它必须首先是Python语法。此外,由于这是来自JSON调用的结果,请注意,这不是一个好的做法,因为JSON并不总是能够转换为Python。具体来说,true、false和null在Python中不是有效的值,会导致错误。除此之外,如果您对接收到的对象不是100%自信,那么这里存在严重的注入安全漏洞。

所有这些都说完了,如果你走投无路,感到绝望:

import ast
x = ast.literal_eval('{"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}')

在我的电脑上可以运行。


我尝试过eval(r.json()),但这对我来说没有意义。我也不认为eval(r)会解决问题,因为r是一个对象。我对eval()不太熟悉,但它似乎更适合执行包含Python命令的字符串,例如eval("2+2")。我正在尝试使用json.loads(data)创建一个可以循环遍历的字典。 - guyja
对的,eval将一个字符串评估为一个对象。你的json字符串已经是一个字典,只需要被评估为一个Python对象。我在上面打错了变量,正在编辑。 - SuperFamousGuy
-1 eval 不是 JSON 解析器的替代品。不仅已经有一个完美的 JSON 解析器,而且 JSON 与 Python 语法不兼容(首先,truefalsenull 都不是有效的 Python),eval 是不安全和过于宽容的。 - user395760

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接