How to get this JSON data

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

How to get this JSON data

05 Sep 2020, 14:06

I want to get the domains, for example the first is ftrertre

I am including the exact data format that is returned

Code: Select all

o =
(
[
  {
    "domain": "ftrertre"
  },
  {
    "domain": "rer"
  },
  {
    "domain": "hkjhkh"
  },
  {
    "domain": "bmgm"
  },
  {
    "domain": "wsfg"
  },
  {
    "domain": "iuyhrtr"
  },
  {
    "domain": "uytnjtf"
  }
]
)
obj := JsonToAHK(o)
MsgBox, % obj.length() ; this works  to get number of domains
ExitApp


JsonToAHK(json, rec := false) {
   static doc := ComObjCreate("htmlfile")
         , __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
         , JS := doc.parentWindow
   if !rec
      obj := JsonToAHK(JS.eval("(" . json . ")"), true)
   else if !IsObject(json)
      obj := json
   else if json.toString() != "[object Object]" {
      obj := []
      Loop % json.length
         obj.Push( JsonToAHK(json[A_Index - 1], true) )
   }
   else {
      obj := {}
      keys := JS.Object.keys(json)
      Loop % keys.length {
         k := keys[A_Index - 1]
         obj[k] := JsonToAHK(json[k], true)
      }
   }
   Return obj
}
User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: How to get this JSON data

05 Sep 2020, 14:47

Jxon library: https://github.com/cocobelgica/AutoHotkey-JSON/blob/master/Jxon.ahk.

Code: Select all

#Include Jxon.ahk
o =
(
    {
        "domain": "ftrertre"
    }
)
data := jxon_load(o)
Msgbox % data.domain
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: How to get this JSON data

08 Sep 2020, 17:43

It's a linear array of assoc. arrays (consisting of one key-value pair each):

Code: Select all

obj := JsonToAHK(o)
MsgBox, % obj.length() ; this works  to get number of domains
msgbox % obj[1].domain
for idx, elem in obj
	msgbox % "domain " idx " : " elem.domain

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750, Rohwedder and 285 guests