load associative array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

load associative array

Post by ananthuthilakan » 17 May 2021, 00:01

i have this in my script, i am trying to somehow save that array to a file and read it from there

Code: Select all


test:={"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}}
msgBox,%test ; gives blank value
msgBox,%test.general.1 ; give correct value ----> this is the required output i am looking for

i can save it but when using file read i think it gives something like this in effect

Code: Select all

test={"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}}
msgBox,%test ; gives whole string
msgBox,%test.general.1 ; gives blank value ---> and fails
any input will be much appreciated



i tried fileread.

{"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}} ; this is saved as file.txt

Code: Select all

FileRead, test, file.txt
msgBox,% test ; gives the whole thing as string
msgBox,% test.general.1  ; gives blank , i am trying to get "name"
Last edited by ananthuthilakan on 17 May 2021, 01:34, edited 1 time in total.
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: load associative array

Post by ananthuthilakan » 17 May 2021, 01:26

i have this in my script, i am trying to somehow save that array to a file and read it from there

Code: Select all


test:={"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}}
msgBox,%test ; gives blank value
msgBox,%test.general.1 ; give correct value ----> this is the required output i am looking for

i can save it but when using file read i think it gives something like this in effect

Code: Select all

test={"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}}
msgBox,%test ; gives whole string
msgBox,%test.general.1 ; gives blank value ---> and fails
any input will be much appreciated
Last edited by ananthuthilakan on 17 May 2021, 01:33, edited 1 time in total.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: load associative array  Topic is solved

Post by AHKStudent » 17 May 2021, 01:46

@teadrinker 's json parse

Code: Select all

test = {"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}}
getObj := jsonToAHK(test)
MsgBox, % getObj.general.1
MsgBox, % getObj.page.current
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
}
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: load associative array

Post by ananthuthilakan » 17 May 2021, 02:21

thankyou for the reply, i tried it earlier but was not working , but after your reply i gave it more time, and realized i should enclose the keys ie 1, 2, 3 in my array with doubequotes , then can be made it to work with json library
thank you , much appreciated :thumbup:
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: load associative array

Post by ananthuthilakan » 17 May 2021, 02:27

AHKStudent wrote:
17 May 2021, 01:46
@teadrinker 's json parse

Code: Select all

test = {"general":{ 1: "name", 2: "author", 3: "edition", 4: "publication", 5: "price"},"page":{total: 720, current:20}}
getObj := jsonToAHK(test)
MsgBox, % getObj.general.1
MsgBox, % getObj.page.current
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
}
thank you for the help , this is much simpler and i dont have to change the keys also (edit : if the key is a string i have to put it in double quotes :thumbup: ) :bravo:, i suppose i should go with this one rather than json library, what is your thoughts on it in terms of performance
Last edited by ananthuthilakan on 17 May 2021, 02:35, edited 1 time in total.
gregster
Posts: 9012
Joined: 30 Sep 2013, 06:48

Re: load associative array

Post by gregster » 17 May 2021, 02:30

My guess is that teadrinker's approach is generally faster - but I didn't test it.
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: load associative array

Post by ananthuthilakan » 17 May 2021, 02:38

much appreciative to your replies , this was so helpful thank you :bravo:
Post Reply

Return to “Ask for Help (v1)”