Process JSON good and Loop Parse question

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Process JSON good and Loop Parse question

24 Sep 2021, 11:00

Here is my function for processing JSON text in the way i usually do in PHP.

  • Question 1: Why using n++ has to be placed inside loop parse parentheses and if i put it inside for parentheses it fails to increment?
  • Question 2: I'd love to hear some criticism for my implementation of this function, what could you improve?
  • Note: I had to use strSplit() because it threats Delimiters parameter as expression and loop parse threats Delimiters parameter as list of separate characters.

Code: Select all

processJsonArr(JsonArr) {

	resultArr := Map()

	n := 1

	for key, val in JsonArr {
		
		;strip multi demensional JSON arrays, only keep first dimension
		removeString := "{" . GetSubString(val, "{", "}") . "}"
		val := strReplace(val,removeString, "0")
		
		Loop Parse, val, "," {
			tempArr := strSplit(A_LoopField, '":')
			
			if (tempArr.has(1) && tempArr.has(2)) {
				resultArr[n] := Map()
				resultArr[n][Trim(tempArr[1], '"')] := Trim(tempArr[2], '"') 
				
				n++
			}

		}
	}

	return resultArr
} 
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Process JSON good and Loop Parse question

24 Sep 2021, 11:20

  1. it doesnt
  2. what do u imagine randoms should tell u about a function whose inputs are unknown, dependencies are unknown, purpose is unknown and outputs are unknown? is there something to improve? 🤷‍♀️
RomAnT
Posts: 21
Joined: 23 Sep 2021, 13:19

Re: Process JSON good and Loop Parse question

25 Sep 2021, 09:05

swagfag wrote:
24 Sep 2021, 11:20
  1. it doesnt
n++ need to be placed exactly as i explained otherwise it does not increment. I am trying to understand why.
vmech
Posts: 356
Joined: 25 Aug 2019, 13:03

Re: Process JSON good and Loop Parse question

25 Sep 2021, 10:07

RomAnT wrote:
25 Sep 2021, 09:05
swagfag wrote:
24 Sep 2021, 11:20
  1. it doesnt
n++ need to be placed exactly as i explained otherwise it does not increment. I am trying to understand why.
Try n += 1 instead.
Operators
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Process JSON good and Loop Parse question

25 Sep 2021, 10:16

vmech wrote: Try n += 1 instead.
Why would that produce a different result?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Process JSON good and Loop Parse question

25 Sep 2021, 11:43

its simple. post the contents of JsonArr, post the definition of GetSubString, post ur expected value for resultArr and u can rest assured everything will be demystified at once

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: CraigM, niCode, redrum, sofista and 35 guests