JSON Reddit

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

JSON Reddit

24 Oct 2014, 15:56

How can I take the json file from reddit and separate each element into it's own variable, for a limit more than 1? I began my attempt with XML, but the JSON file offers much more information that I want, but I don't know how to work with it. Can someone start me off in the right direction? Thanks!

https://www.reddit.com/.json?limit=2
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: JSON Reddit

24 Oct 2014, 16:23

here is an example

Code: Select all

#Include JSON.ahk
#Persistent


;Prepare our WinHttpRequest object
HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpObj.SetTimeouts(6000,6000,6000,6000) ;Set timeouts to 6 seconds

url := "https://www.reddit.com/.json?limit=2"
HttpObj.Open("GET",URL)
HttpObj.Send()

sleep 200

j := json.parse(HttpObj.Responsetext) ; convert JSON Response to AHK object

msgbox % "1: " j.data.children.1.data.title "`n2: " j.data.children.2.data.title

return
I use Coco's Json class there are others but this works for me

Hope it helps
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: JSON Reddit

27 Oct 2014, 12:12

I'm having trouble using a Loop with A_Index. Any suggestions?

Code: Select all

Limit := "2"

HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpObj.SetTimeouts(6000,6000,6000,6000)

url := "https://www.reddit.com/.json?limit=%Limit%"
HttpObj.Open("GET",URL)
HttpObj.Send()

Sleep, 200

j := json.parse(HttpObj.Responsetext)

Loop, %Limit%
{
	MsgBox, % j.data.children.A_Index.data.title
}
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: JSON Reddit

27 Oct 2014, 13:32

example:

Code: Select all

#Include JSON.ahk
Limit := "2"

HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
HttpObj.SetTimeouts(6000,6000,6000,6000)

url := "https://www.reddit.com/.json?limit=" Limit
HttpObj.Open("GET",URL)
HttpObj.Send()

Sleep, 200

j := json.parse(HttpObj.Responsetext)

Loop, %Limit%
{
    MsgBox, % j.data.children[A_index].data.title
}
Hope it helps
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: JSON Reddit

27 Oct 2014, 13:47

Thanks, that's perfect! I ended up getting what you have, all except for removing a period before [A_Index]. Much appreciated!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Giresharu, Google [Bot], inseption86, jomaweb, Rohwedder and 269 guests