Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

如何使用python提取json中指定字段的数据?

现在有一个json数据,
如下:

{
   "favourite":{
      "bkmrk":{
         "id1490843709594066":{
            "guid":"904eff52277f403b89f6410fe2758646.11",
            "lcate":"1"
         },
         "id1490843712805183":{
            "guid":"58457f60eca64025bc43a978f9c98345.16",
            "lcate":"2"
         },
         "id149084371467327":{
            "guid":"a0f907f9dc8b40f689b083f3eba7228b.16",
            "lcate":"3"
         },
         "id1490843716295393":{
            "guid":"eb75d929455e468bb712e7bc2025d11a.16",
            "lcate":"4"
         }
      }
   }
}

我该如何使用python操作该json,才能输出所有的guid和lcate有序对,即以下形式:

1;904eff52277f403b89f6410fe2758646.11
2;58457f60eca64025bc43a978f9c98345.16
3;a0f907f9dc8b40f689b083f3eba7228b.16
4;eb75d929455e468bb712e7bc2025d11a.16

非常感谢。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
json_dict = json.loads(json_str)

for item in json_dict['favourite']['bkmrk']:
    guid = json_dict['favourite']['bkmrk'][item]['guid']
    lcate = json_dict['favourite']['bkmrk'][item]['lcate']
    print '%s:%s' % (lcate,guid)

反正都是字典,不要想那么多


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...