Page 1 of 1

[JSON]How to Get the Second Level Value?

Posted: Wed Sep 11, 2019 12:40 pm
by Benares
like this json:

{"status":1,"info":"OK","data":{"uid":123,"token":"012345678901234567890123456789"}}


How to Get the Second Level, "uid","token" Value?

Thanks a Lot!

Re: [JSON]How to Get the Second Level Value?

Posted: Wed Sep 11, 2019 12:44 pm
by kenmo
Use GetJSONMember() twice:

Code: Select all

Text.s = "{'status':1,'info':'OK','data':{'uid':123,'token':'012345678901234567890123456789'}}"

Text = ReplaceString(Text, "'", #DQUOTE$)
If ParseJSON(0, Text)

  ; Showing step by step... you could combine these and use less variables
  *MainObject = JSONValue(0)
  *Data = GetJSONMember(*MainObject, "data")
  *Token = GetJSONMember(*Data, "token")
  Token.s = GetJSONString(*Token)
  Debug "Token: " + Token
Else
  Debug "Parse failed"
EndIf

You can also use my JSON_Helper file:
https://github.com/kenmo-pb/includes/bl ... Helper.pbi

Code: Select all

IncludeFile "JSON_Helper.pbi"

Text.s = "{'status':1,'info':'OK','data':{'uid':123,'token':'012345678901234567890123456789'}}"

Text = ReplaceString(Text, "'", #DQUOTE$)
If ParseJSON(0, Text)
  Debug "Token: " + JSONStringFromPath(MainJSONObject(0), "data/token")
  Debug "UID: " + JSONIntegerFromPath(MainJSONObject(0), "data/uid")
Else
  Debug "Parse failed"
EndIf

Re: [JSON]How to Get the Second Level Value?

Posted: Wed Sep 11, 2019 1:03 pm
by Benares
Thank You Very Much!

Re: [JSON]How to Get the Second Level Value?

Posted: Wed Sep 11, 2019 1:26 pm
by infratec
Or:

Code: Select all

Structure UidTokenStructure
  uid.i
  token.s
EndStructure

Structure JSONStructure
  status.i
  info.s
  Data.UidTokenStructure
EndStructure


Define JSON$, JSONStruct.JSONStructure

JSON$ = ~"{\"status\":1,\"info\":\"OK\",\"Data\":{\"uid\":123,\"token\":\"012345678901234567890123456789\"}}"
Debug JSON$

If ParseJSON(0, JSON$)
  ExtractJSONStructure(JSONValue(0), @JSONStruct, JSONStructure)
  Debug JSONStruct\Data\uid
  Debug JSONStruct\Data\token
  FreeJSON(0)
EndIf