Help with Json

luiswagnersantos

Member
Licensed User
Longtime User
Hi, Friends.

I'm trying to solve the problem is my code or Json.
I'm using HttpClient to get Json

See Valid Json.
jsonvalid.jpg


B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim URL As String
    'URL = "http://192.168.100.5:8080/ServerFunctionInvoker"
   URL="http://192.168.1.103:8080/datasnap/rest/TServicoVenda/getCliente(1)"
   Dim HttpClient1 As HttpClient
   'Dim sJSONData   As String 
   
End Sub

Sub Globals
   Dim sJSONData   As String
   Dim Load As Button
   Dim Parse As Button
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   HttpClient1.Initialize("HttpClient1")
   Activity.LoadLayout("json1") 'Load the layout file.
End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Log("ResponseSuccess")
   ProgressDialogHide
   Dim result As String
   result = Response.GetString("UTF8") 'Convert the response to a string
   sJSONData = result
   Log(result) ' test dump
End Sub

Sub HttpClient1_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
   Log(Reason)
   Log(StatusCode)
   ProgressDialogHide
   msg = "Error connecting to server."
   If Reason <> Null Then msg = msg & CRLF & Reason
   ToastMessageShow (msg, True)
End Sub

Sub GetJSONData()
    Dim request As HttpRequest
   request.InitializeGet(URL)
   request.Timeout = 10000 'set timeout to 10 seconds
   If HttpClient1.Execute(request, 1) = False Then Return 'Will be false if their is already a running task (with the same id).
   ProgressDialogShow("Calling server...")

End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Parse_Click
   LoadJSONDATA
End Sub

Sub Load_Click
    GetJSONData
End Sub

The result is this:

B4X:
{"result":[{"id":1,"nome":"Jonh Carter","cpf":"123.567.789.09"}]}

I'm not able to read the object. Could someone lend a hand and show what I am doing wrong?

B4X:
Sub LoadJSONDATA()

Log("Data:"&sJSONData)
 
Dim JSON As JSONParser
Dim Map1 As Map
JSON.Initialize(sJSONData)
Map1 = JSON.nextobject

Log(sJSONData)
Log(JSON)
Log(Map1)

End Sub
Result Log
B4X:
ResponseSuccess
{"result":[{"id":1,"nome":"Jederson Zuchi","cpf":"123.456.789-10"}]}
Data:{"result":[{"id":1,"nome":"Jederson Zuchi","cpf":"123.456.789-10"}]}
{"result":[{"id":1,"nome":"Jederson Zuchi","cpf":"123.456.789-10"}]}
(JSONTokener)  at character 68 of {"result":[{"id":1,"nome":"Jederson Zuchi","cpf":"123.456.789-10"}]}
(MyMap) {result=[{id=1, cpf=123.456.789-10, nome=Jederson Zuchi}]}
ResponseSuccess
{"result":[{"id":1,"nome":"Jonh Carter","cpf":"123.567.789.09"}]}

How can I map?

Tank You
 
Last edited:

luiswagnersantos

Member
Licensed User
Longtime User
Array Json - some questions

Erel, believe I'm having problems with my generator Json.
Could you help me with this json string?
how do I mapped it?

{"result":[[{"ID":"1","NOME":"Jonh Carter","CPF":"123.567.789.09"},{"ID":"2","NOME":"Paulo Quicoli","CPF":"987.654.321-24"},{"ID":"3","NOME":"Giuliano Combatti soares","CPF":"121.212.121-88"}]]}

Validating json string looks like this right
In my generated have 2 brackets. That is my question

validjson1709.jpg


Tanks

Wagner
 
Upvote 0

leonardoffsilva

Member
Licensed User
Longtime User
Luis, I was having the same problem. The 2 brackets creates an List inside a list.
You can remove the 2 brackets with an "string replace" or read the first item of the list to an other.

My code was created by a Datasnap server, like yours!
 
Upvote 0
Top