ExtractJSONMap problem?

Just starting out? Need help? Post your questions and find answers here.
goomoo
Posts: 11
Joined: Fri Sep 04, 2015 4:14 pm

ExtractJSONMap problem?

Post by goomoo »

ExtractJSONMap problem?
I want to use ExtractJSONMap, but in the second layer, I get [Object Object], I need to get "loginName", how do I get that?

Code: Select all

s.s="{`id`:0,`sMessage`:`ok`,`object`:{`_explicitType`:`model.t_user`,`id_user`:`538`,`loginName`:`asdf`,`loginPassHash`:`827ccb0eea8a706c4c34a16`,`displayName`:`\u963f\u65af\u987f\u53d1`,`motto`:``,`email`:``,`money`:`30.50`,`regIP`:`127.0.0.1`,`regTime`:`2021-09-27 11:15:04`,`loginCount`:`0`,`lastLoginTime`:`1900-01-01 00:00:00`,`lastLoginIP`:`0.0.0.0`,`thisLoginTime`:`1900-01-01 00:00:00`,`thisLoginIP`:`0.0.0.0`,`UserMemo`:``,`iIsAdmin`:`0`,`0`:`538`,`1`:`asdf`,`2`:`827ccb0eea8a706c4c34a16`,`3`:`\u963f\u65af\u987f\u53d1`,`4`:``,`5`:``,`6`:`0`,`7`:`127.0.0.1`,`8`:`2021-09-27 11:15:04`,`9`:`0`,`10`:`1900-01-01 00:00:00`,`11`:`0.0.0.0`,`12`:`1900-01-01 00:00:00`,`13`:`0.0.0.0`,`14`:``,`15`:`0`}}";
s=ReplaceString(s,"`",Chr(34)) ;Chr(34)="
Debug s
ParseJSON(0, s)
ObjectValue = JSONValue(0)

  
NewMap Options()
ExtractJSONMap(JSONValue(0), Options())       
  
Debug Options("id")
Debug Options("sMessage")
Debug Options("object")
; 0
; ok
; [object Object]

user=Options("object")

NewMap userMap()
;ExtractJSONMap(user, userMap()) ;<-- not correct
;userMap=user ;<-- not correct
Debug userMap("_explicitType")
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: ExtractJSONMap problem?

Post by Peter »

You can use structures and ExtractJSONStructure() for this. Then you can access the values comfortably:

Code: Select all

EnableExplicit

Structure sObject
  
  _explicitType.s
  id_user.s
  ; and so on...
  
EndStructure

Structure sFoo
  
  id.i
  sMessage.s
  object.sObject
  
EndStructure

Define Foo.sFoo
Define s.s

s = "{`id`:0,`sMessage`:`ok`,`object`:{`_explicitType`:`model.t_user`,`id_user`:`538`,`loginName`:`asdf`,`loginPassHash`:`827ccb0eea8a706c4c34a16`,`displayName`:`\u963f\u65af\u987f\u53d1`,`motto`:``,`email`:``,`money`:`30.50`,`regIP`:`127.0.0.1`,`regTime`:`2021-09-27 11:15:04`,`loginCount`:`0`,`lastLoginTime`:`1900-01-01 00:00:00`,`lastLoginIP`:`0.0.0.0`,`thisLoginTime`:`1900-01-01 00:00:00`,`thisLoginIP`:`0.0.0.0`,`UserMemo`:``,`iIsAdmin`:`0`,`0`:`538`,`1`:`asdf`,`2`:`827ccb0eea8a706c4c34a16`,`3`:`\u963f\u65af\u987f\u53d1`,`4`:``,`5`:``,`6`:`0`,`7`:`127.0.0.1`,`8`:`2021-09-27 11:15:04`,`9`:`0`,`10`:`1900-01-01 00:00:00`,`11`:`0.0.0.0`,`12`:`1900-01-01 00:00:00`,`13`:`0.0.0.0`,`14`:``,`15`:`0`}}";

s = ReplaceString(s,"`",Chr(34))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ;Chr(34)="

If ParseJSON(0, s)
  
  ExtractJSONStructure(JSONValue(0), @Foo, sFoo)
  
  Debug "id: " + Foo\id
  Debug "sMessage: " + Foo\sMessage
  Debug "object\_explicitType: " + Foo\object\_explicitType
  Debug "object\id_user: " +  Foo\object\id_user
  ; and so on...
  
  FreeJSON(0)
  
EndIf
goomoo
Posts: 11
Joined: Fri Sep 04, 2015 4:14 pm

Re: ExtractJSONMap problem?

Post by goomoo »

I tried it and it worked. Thank you. :)
Post Reply