[JSON]How to Get the Second Level Value?

Just starting out? Need help? Post your questions and find answers here.
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

[JSON]How to Get the Second Level Value?

Post 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!
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

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

Post 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
Benares
User
User
Posts: 16
Joined: Sun Aug 18, 2013 10:53 pm

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

Post by Benares »

Thank You Very Much!
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

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

Post 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
Post Reply