Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
squirreltown
Roku Guru

XML to JSON

I would like to add some twitter functionality to my channel so my first step is needing to "fix" the SDK twitterOauth example which is currently broken since Twitter upgraded their API and now uses JSON instead of XML. My question for this first function is what is the JSON equivalent of xml.GetChildElements() since what i have there is not working
Function parse_status_response(json As Object, tweetArray As Object) As Void

tweetElements = json.GetChildElements()
tweetCount = 0

for each tweet in tweetElements

item = init_tweet_item()

item.Message = validstr(tweet.text.GetText())
item.UserName = validstr(tweet.user.screen_name.GetText())
item.ImageSource = validstr(tweet.user.profile_image_url.GetText())

tweetCount = tweetCount + 1
tweetArray.Push(item)
end for

End Function


And secondly Is there anything really obviously wrong with the start of this function (although the debugger wont let me get to this until the first question gets answered)

Function show_tweet_canvas() As Void

http = NewHttp(m.prefix+"1.1/statuses/user_timeline.json?screen_name=RokuPlayer")
oa = Oauth()
oa.sign(http,false)
rsp = http.getToStringWithTimeout(10)

json = ParseJson(rsp)

tweetArray = CreateObject("roArray", 100, true)
m.ParseTweets(json, tweetArray)
-----------------------




BTW the Oauth part is working fine.

Thanks
Kinetics Screensavers
0 Kudos
10 REPLIES 10
RokuMarkn
Visitor

Re: XML to JSON

The return value from ParseJSON is just a normal AssociativeArray. You should be able to say

for each tweet in json


--Mark
0 Kudos
squirreltown
Roku Guru

Re: XML to JSON

Thanks Mark but i'm not clear how to implement that, with xml the hierarchy was:

xml
tweetElements = xml.getchildElements()
for each tweet in tweetElements


switching json for xml i have:

json
tweetElements =? (what goes here? another "for each"? And for each what of what?
for each tweet in tweetElements'


I changed
 tweetElements = xml.GetChildElements()

to
tweetElements = json

which get me past that point but I'm getting a blank canvas no tweets yet.
I am getting this message in the debugger and cant find what 400 means -
Http: # 5975 done status: 400 

Thanks
Kinetics Screensavers
0 Kudos
RokuChris
Roku Employee
Roku Employee

Re: XML to JSON

"squirreltown" wrote:
Thanks Mark but i'm not clear how to implement that, with xml the hierarchy was:

xml
tweetElements = xml.getchildElements()
for each tweet in tweetElements


switching json for xml i have:

json
tweetElements =? (what goes here? another "for each"? And for each what of what?
for each tweet in tweetElements


It's not as simple as switching xml for json. They are different types, like Mark said. Parsing XML gives you a roXMLElement with its own set of member functions. ParseJSON() returns a plain old roAssociativeArray, so you can work with it like you would any other roAssociativeArray. roAssociativeArray doesn't have a function equivalent to GetChildElements(). To step through the members of a roAssociativeArray, you'd do something like:

for each key in myAA
? key + ":" + myAA.Lookup(key) ' if the value isn't a string, this won't work, but you get the idea
next


Somewhere in the SDK sample code should be a function called PrintAA(). If you use that function on the result you get from ParseJSON(), you might get a better idea of what the structure looks like.
0 Kudos
RokuMarkn
Visitor

Re: XML to JSON

"squirreltown" wrote:

I changed
 tweetElements = xml.GetChildElements()

to
tweetElements = json


That's fine, although you don't really need the tweetElements variable. You can just write the for statement the way I quoted it above.

"squirreltown" wrote:

which get me past that point but I'm getting a blank canvas no tweets yet.
I am getting this message in the debugger and cant find what 400 means -
Http: # 5975 done status: 400 



Status 400 is "Bad Request". It means the server is rejecting your request for some reason. You're sending something that twitter doesn't like.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html has a list of all HTTP error codes.

--Mark
0 Kudos
squirreltown
Roku Guru

Re: XML to JSON

Thank You Chris and Mark. I will work with it some more and I'm sure I'll be back. Chris, I have used that PrintAA function before, thats a real good idea, and thank you Mark for the codes, thats useful.
Kinetics Screensavers
0 Kudos
squirreltown
Roku Guru

Re: XML to JSON

I am getting tweets back from twitter now but don't know how to organize what I'm receiving. In the original XML version there is a variable "tweet", that is never defined anywhere in the channel. I cant run the channel the old (xml) way but i suspect "tweet" was a heading in the XML output from twitter. If that is so, its not there any more so I'm not sure how to replace it.
If i retrieve 2 tweets like this:
json = ParseJson(rsp)

printing "json" gives me each tweet as a seperate array like this:
<Component: roAssociativeArray>
<Component: roAssociativeArray>

I cant use PrintAA because of non-numeric index but i can write to ascii file and print that. Here is one tweet to RokuChris:
[{"created_at":"Sat Apr 06 16:12:20 +0000 2013","id":XXXX,"id_str":"XXXX","text":"@nowhereman Its working now thank you! Only issue it seems to run at 120% speed - watchable but chipmunk-y. Just me?","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":XXXX,"in_reply_to_user_id_str":"XXXX","in_reply_to_screen_name":"nowhereman","user":{"id":XXXX,"id_str":"XXXX","name":"XXXX","screen_name":"XXXX","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":0,"friends_count":0,"listed_count":0,"created_at":"Wed Oct 12 19:06:56 +0000 2011","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":3,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/3485369747\/f9369ecfc20f767fd665d7beab820ea7_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/3485369747\/f9369ecfc20f767fd665d7beab820ea7_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"nowhereman","name":"Nowhere Man","id":XXXX,"id_str":"XXXX","indices":[0,11]}]},"favorited":false,"retweeted":false,"lang":"en"}]

Here is the original XML function I'm fighting with:
Function parse_status_response(xml As Object, tweetArray As Object) As Void

tweetElements = xml.GetChildElements()
tweetCount = 0

for each tweet in tweetElements

item = init_tweet_item()

item.Message = validstr(tweet.text.GetText())
item.UserName = validstr(tweet.user.screen_name.GetText())
item.ImageSource = validstr(tweet.user.profile_image_url.GetText())

tweetCount = tweetCount + 1
tweetArray.Push(item)
end for

End Function


And my current not working yet version:
Function parse_status_response(json As Object, tweetArray As Object) As Void

tweetCount = 0

for each tweet in json 'obviously wrong

item = init_tweet_item()

item.Message = validstr(tweet.text.GetText())
item.UserName = validstr(tweet.user.screen_name.GetText())
item.ImageSource = validstr(tweet.user.profile_image_url.GetText())

tweetCount = tweetCount + 1
tweetArray.Push(item)
end for

End Function


So, in order to say "for each", what do I call each "key" in that twitter output?
How to replace the "tweet" variable in the original?
Each tweet is a separate array - did it come that way when it was xml? Do i need to add some array processing here?
If my variable "json" is the entire twitter output (multiple tweets) How do i write a variable that represents each array? like Json[array] or something.

Thanks
Kinetics Screensavers
0 Kudos
RokuMarkn
Visitor

Re: XML to JSON

I misspoke earlier, the return value from ParseJSON is an roArray not an AA. So to look at the first item you just say "json[0]". The second is "json[1]", etc. Each of these items is an AA. The foreach statement will step through them. You can use PrintAA to examine the structure of each one: PrintAA(json[0]) etc.

You probably want something like


for each tweet in json
item.Message = tweet["text"] ' or tweet.text
item.UserName = tweet["user"]["name"] ' or tweet.user.name


BTW if you're unsure of what a variable is, it's handy to do "print type(var)" in the debugger.

--Mark
0 Kudos
squirreltown
Roku Guru

Re: XML to JSON

Thank you Mark. Between your guidance and flat-out guessing I've made it work. I will post the modified functions in a different thread if that can save anyone some effort.
Have one remaining issue. I want to add any included photos with the tweets, but the key I need is 5 nested arrays deep and that seems to cause a syntax error if you go more than 3 levels (2 dots)

What i need is:

tweet.entities.media.List(0).media_url

Is there a special way to handle this?

Thanks

AA printout below
---- AA ----
retweeted: false
source: <a href="http://www.apple.com" rel="nofollow">Photos on iOS</a>
favorited: false
lang: en
coordinates: invalid
geo: invalid
in_reply_to_status_id: invalid
place: invalid
in_reply_to_screen_name: invalid
in_reply_to_status_id_str: invalid
retweet_count: 0
possibly_sensitive: false
entities: (assocarr)...
hashtags: (list of 0)...
urls: (list of 0)...
user_mentions: (list of 0)...
media: (list of 1)...
List(0)= (assocarr)...
expanded_url: http://twitter.com/XXXX/status/216669998213636096/photo/1
media_url_https: https://pbs.twimg.com/media/AwHENyVCIAAT_eL.jpg
display_url: pic.twitter.com/A3YDa8aT
sizes: (assocarr)...
small: (assocarr)...
w: 340
resize: fit
h: 453
thumb: (assocarr)...
w: 150
resize: crop
h: 150
large: (assocarr)...
w: 384
resize: fit
h: 512
medium: (assocarr)...
w: 384
resize: fit
h: 512
media_url: http://pbs.twimg.com/media/AwHENyVCIAAT_eL.jpg
Kinetics Screensavers
0 Kudos
TheEndless
Channel Surfer

Re: XML to JSON

"squirreltown" wrote:
tweet.entities.media.List(0).media_url

Your syntax error is most likely the parentheses. Those should be square brackets.

EDIT: Actually, looking at the output, your brackets/parentheses are at the wrong level. I believe what you're looking for is:
tweet.entities.media[0].media_url
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos