Receive Php Associative Arrays : HTTPRequest / ExamineJSON

Share your advanced knowledge/code with the community.
falsam
Posts: 280
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Receive Php Associative Arrays : HTTPRequest / ExamineJSON

Post by falsam »

:arrow: Script Php

Code: Select all

<?php
	$array = array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
	echo json_encode($array);
?>
:arrow: Code SpiderBasic

Code: Select all

Enumeration
  #Json
EndEnumeration

Procedure HttpGetEvent(Success, Result$)
  Protected i
  
  If Success
    
    Debug Result$
    
    If ParseJSON(#Json, Result$)  
      ObjectValue = JSONValue(#Json)
  
      If ExamineJSONMembers(ObjectValue)
        While NextJSONMember(ObjectValue)
          Debug JSONMemberKey(ObjectValue) + " = " + GetJSONInteger(JSONMemberValue(ObjectValue))
        Wend
      EndIf
    EndIf   
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

HTTPRequest(#PB_HTTP_Get, "receivedata3.php", "", @HttpGetEvent())
:arrow: Test: http://s242132022.onlinehome.fr/sb-demophp3/index.html

➽ Windows 11 - JDK 1.8 - SB 2.40 - Android 13
http://falsam.com

Sorry for my poor english
User avatar
freak
Posts: 7
Joined: Mon Feb 24, 2014 10:26 pm
Location: Germany

Re: Receive Php Associative Arrays : HTTPRequest / ExamineJS

Post by freak »

You can also get the data directly into a PB Map like this:

Code: Select all

Enumeration
  #Json
EndEnumeration

Procedure HttpGetEvent(Success, Result$)
  Protected i
 
  If Success
   
    Debug Result$
   
    If ParseJSON(#Json, Result$) 
      ; transfer the php array to a map
      NewMap Values.l()
      ExtractJSONMap(JSONValue(#Json), Values())
      
      ; display the result
      ForEach Values()
        Debug MapKey(Values()) + " = " + Values()
      Next Values()
    EndIf 
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

HTTPRequest(#PB_HTTP_Get, "receivedata3.php", "", @HttpGetEvent())
falsam
Posts: 280
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: Receive Php Associative Arrays : HTTPRequest / ExamineJS

Post by falsam »

Thank you freak. This solution is more interesting than mine.

➽ Windows 11 - JDK 1.8 - SB 2.40 - Android 13
http://falsam.com

Sorry for my poor english
Post Reply