How to move to next JSON map ?

Qbetechmark

Member
Licensed User
Longtime User
This is my return JSON from PHP :

B4X:
[{"ID":"21","username":"tedy","password":"888"},{"ID":"23","username":"tom","password":"cat"},{"ID":"22","username":"bunny","password":"carrot"}]

This is my code in B4A :

B4X:
Sub Globals
    Dim hc As HttpClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        hc.Initialize("hc")
    End If
End Sub

Sub ButtonOk_Click
    Dim post As HttpRequest
    post.InitializeGet("http://192.168.0.11/testandroid/where.php")
    hc.Execute(post, 2)
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    If TaskId = 1 Then
                'Do some other code
   Else If TaskId =2 Then
      ToastMessageShow("Processing", True)
      Dim result,Uname, Pwd, Rtext As String
      Dim JSON As JSONParser
      Dim m As Map
      Dim ID, zie, i As Int
      
      result = Response.GetString("UTF8")
      result = result.SubString2( 3, result.Length - 3) 
      JSON.Initialize(result)
      m = JSON.NextObject 
      zie = m.Size

      For i = 0 To m.Size - 1 
         m.Get(i)
         Rtext = Rtext & m.Get("username") & ";" & m.Get("ID") & ";" & m.Get("password") & CRLF
      Next
    End if
End Sub

I'm expecting Rtext will return :
tedy;21;888
tom;23;cat
bunny;22;carrot


But I got Rtext :
tedy;21;888
tedy;21;888
tedy;21;888


How to move to next data in JSON map ? What did I do wrong ?
 

Qbetechmark

Member
Licensed User
Longtime User
O yeah ... you're right Erel.
This is my correct code :

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("l1")
   
   If FirstTime Then
      Dim post As HttpRequest
       hc.Initialize("hc")
      post.InitializeGet("http://192.168.0.10/testandroid/where.php")
      hc.Execute(post, 1)
        'It will returns : [{"ID":"21","username":"tedy","password":"888"},{"ID":"23","username":"tom","password":"cat"},{"ID":"22","username":"bunny","password":"carrot"}]
    End If
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    If TaskId = 1 Then
      Dim JSON As JSONParser
      Dim L1 As List
      Dim Result, ReturnString As String 
      Dim i As Int
      Dim M1 As Map
      
      Result = Response.GetString("UTF8")
      Label1.Text = Result
      
      JSON.Initialize(Result)
      L1 = JSON.NextArray   
      For i = 0 To L1.Size - 1
         M1 = L1.Get(i)
         ReturnString = ReturnString & M1.Get("ID") & " : " & M1.Get("username") & " : " & M1.Get("password") & CRLF
      Next
      
      Label1.Text = ReturnString
   End If
End Sub

Thank you Erel :sign0098:
 
Upvote 0

ttsolution

Member
Licensed User
Longtime User
Dear all,

Please help me how to parse below JSON to ListView? many thanks for your help.
 
Last edited:
Upvote 0
Top