python - How to work with multiple JSON formats? -


I am playing with a pathon script so that Twitter Twitter can be dragged, parsed and formatted JSON. Locations in specific formats tend to be trending within an array:

  [["created_at": "2010-07-15T22: 31: 11Z", "trend": [{"name": "Trendy", "url": "http://search.twitter.com/search?q=trendy", "query": "trendy"}, ...   

while Daily and weekly Jason format is not:

  {"trends": {"2011-01-14 15:20": [{"name": "#trendy", "event" I am using trends list this dragon:  
 : null, "promoted_content": null, "query": "#trendy"},   

Class tendency: self.query = query Elf.name = name self: __init __ (self, query, name, promoted_content, events, url) #initialize foo = a "trend" object with trend (query, name ...) .promoted_content = promoted_content self.events = Events self.url = url class trending: def __init __ (auto, api_url, title): self.api_url = api_url self.title = title def get_trending (self): import json import urllib2 trends_all as simplejson = json.loads (Urllib2.urlopen (self.api_url) .read () return trends list_trending (self): trends_all = self.get_trending () print "% s \ n"% trends_all [0] ['Trends'] for x self .title: thistrend X ['prognosis'], x ['name'], x ['promoted_content'], x ['events'], x ['url']) print "\ t% s (% s)% s "% (Thistrend.name, thistrend.url, thistrend. Promoted_content)

This works for place formats (first), but not for daily / weekly formats. That's why I am thinking that there is a great way to distinguish two JSON structures and reconstruct them so that I can work with both.

There is a more common case with the list. Since you only need the first element, if the parsed object is a list, you can first remove the value.

In your code, you can change it:

  In x trends_all [0] ['trend']: ...   

  ifinstance (trends_all, list): trends_all = trends_all [0] trend_for all in ['trend']: ...    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -