Given the following json file format:
{"variable1":var1data,"variable2":var2data,"variable3":var3data,"variable4":"var4data","variable5":var5data,"variable6":var6data,"variable7":var7data,"variable8":var8data}
I'm trying, with poor results so far, to read in a file c:\summary.json into a variable array so that it produces:
summary.variable1=var1data
summary.variable2=var2data
etc.
I know this must be simple, and I've read a lot of complex posts, but I still don't get it. Can someone dumb this down for me?
I can get this to work:
summary := {"variable1":var1data,"variable2":var2data,"variable3":var3data,"variable4":"var4data","variable5":var5data,"variable6":var6data,"variable7":var7data,"variable8":var8data}
but I cant get it to work by reading from a file.
Any help is appreciated. Thanks!
Need help reading json file into array variables
Started by
jasdjensen
, Jul 14 2011 07:20 PM
7 replies to this topic
#1
-
Posted 14 July 2011 - 07:20 PM
#2
-
Posted 14 July 2011 - 07:30 PM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Parsing isn't the problem. If I specify the json strings in the code I can parse them without issue.
The problem is reading them from the file into an array. Or maybe I dont understand what you're trying to tell me.
I can even get the json string into a variable with:
FileRead, summaryraw, c:\solar\summary.json
summary := summaryraw
I just cant get it to store it as an array. It just stores it as a string in %summaryraw%
The problem is reading them from the file into an array. Or maybe I dont understand what you're trying to tell me.
I can even get the json string into a variable with:
FileRead, summaryraw, c:\solar\summary.json
summary := summaryraw
I just cant get it to store it as an array. It just stores it as a string in %summaryraw%
#3
-
Posted 14 July 2011 - 07:42 PM
Start by reading from the file into a string. Then ...The problem is reading them from the file into an array.
Based on what you stated, I can only assume you want one of the following:Parsing isn't the problem.
json = {variable1:"var1data",variable2:"var2data"} obj := {} Loop, Parse, json, `,, {} { sep := InStr(A_LoopField, ":") obj[SubStr(A_LoopField,1,sep-1)] := Trim(SubStr(A_LoopField,sep+1), """") } MsgBox, % obj.variable1 "`n" obj.variable2
var1data := "foo" var2data := "bar" json = {variable1:var1data,variable2:var2data} obj := {} Loop, Parse, json, `,, {} { sep := InStr(A_LoopField, ":") VarName := SubStr(A_LoopField,sep+1) obj[SubStr(A_LoopField,1,sep-1)] := %VarName% } MsgBox, % obj.variable1 "`n" obj.variable2
#4
-
Posted 14 July 2011 - 07:56 PM
I'm sorry, I'm sure it's obvious that I'm a novice.
if I do this in a script:
Then I get the results as follows:
But I dont want to specify what the data is, I want to read it from the summary.json file and have it show up in the variable array.
I was thinking I could do something like this to get it from the file, put it into a variable as a string, then treat the string as a variable array:
but that doesn't seem to work like I thought it would.
if I do this in a script:
summary := {"variable1":var1data,"variable2":var2data,"variable3":var3data,"variable4":"var4data","variable5":var5data,"variable6":var6data,"variable7":var7data,"variable8":var8data}
Then I get the results as follows:
summary.variable1=var1data summary.variable2=var2data ..etc
But I dont want to specify what the data is, I want to read it from the summary.json file and have it show up in the variable array.
I was thinking I could do something like this to get it from the file, put it into a variable as a string, then treat the string as a variable array:
FileRead, summaryraw, c:\solar\summary.json summary := summaryraw
but that doesn't seem to work like I thought it would.
#5
-
Posted 14 July 2011 - 08:04 PM
Doesn't work that way :-(
But perhaps this will help json <-> objects <!-- m -->http://www.autohotke...pic.php?t=66070<!-- m -->
But perhaps this will help json <-> objects <!-- m -->http://www.autohotke...pic.php?t=66070<!-- m -->
#6
-
Posted 14 July 2011 - 08:13 PM
Put jethrow's code in a function.
FileRead, summaryraw, c:\solar\summary.json summary := json_parse(summaryraw) Msgbox % summary.variable4 json_parse(json) { obj := {} Loop, Parse, json, `,, {} { sep := InStr(A_LoopField, ":") obj[Trim(SubStr(A_LoopField,1,sep-1), """")] := Trim(SubStr(A_LoopField,sep+1), """") } return obj }
#7
-
Posted 14 July 2011 - 08:13 PM
aboutscript ⋰ apps ⋱ scripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run
Put jethrow's code in a function.
FileRead, summaryraw, c:\solar\summary.json summary := json_parse(summaryraw) Msgbox % summary.variable4 json_parse(json) { obj := {} Loop, Parse, json, `,, {} { sep := InStr(A_LoopField, ":") obj[Trim(SubStr(A_LoopField,1,sep-1), """")] := Trim(SubStr(A_LoopField,sep+1), """") } return obj }
Thanks for making that easy for me. Now it's working. It pulls from the file, inserts the data into the array and looks correct.
Thanks for everyone's help. Sorry I was slow to get it.
#8
-
Posted 14 July 2011 - 08:18 PM