Android Question Problem with JSONparser

Alpay ABAY

Member
Licensed User
I try to run below code, I have other applications with json works without problem.
JSON is not parsing. I could not get to make it work.
Error message is : (JSONTokener) at character 14 of {"id":"BETAT"}
Thanks

B4X:
Dim request As String = $"{"id":"BETAT"}"$
Dim JSON As JSONParser
    Try
        JSON.Initialize(request)
    Catch
        Log(LastException)
    End Try
 
Solution
B4X:
    Dim request As String = $"{"id":"BETAT"}"$
    Dim parser As JSONParser
    parser.Initialize(request)
    Dim jRoot As Map = parser.NextObject
    Dim id As String = jRoot.Get("id")
    Log($"ID = ${id}"$)

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
ID = BETAT
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **

It does work here

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim request As String = $"{"id":"BETAT"}"$
    Dim parser As JSONParser
    parser.Initialize(request)
    Dim jRoot As Map = parser.NextObject
    Dim id As String = jRoot.Get("id")
    Log($"ID = ${id}"$)

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
ID = BETAT
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **

It does work here
 
Upvote 1
Solution

Alpay ABAY

Member
Licensed User
B4X:
    Dim request As String = $"{"id":"BETAT"}"$
    Dim parser As JSONParser
    parser.Initialize(request)
    Dim jRoot As Map = parser.NextObject
    Dim id As String = jRoot.Get("id")
    Log($"ID = ${id}"$)



It does work here

I change my code with yours, and it works now.
But what was the wrong about my code. It is identical?
 
Upvote 0

Alpay ABAY

Member
Licensed User
Maybe my variable names were intersecting with something in b4x. When I changed the variable names it works like charm. Mystery!
Thank you anyway. Case closed.
 
Upvote 0

Alpay ABAY

Member
Licensed User
Again similar problem with JSON parser
Sample json is attached.

B4X:
Private Sub Button3_Click
    Dim str As String = File.ReadString(File.DirApp,"test.txt")
    Log(str)
    Dim parser As JSONParser
    parser.Initialize(str)
    Log(parser)
End Sub


Error message is : (JSONTokener) at character 0 of
Like my content is null or empty. I checked with other tools content is valid json.
but somehow not working on B4J
 

Attachments

  • test.txt
    168 KB · Views: 9
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
File.ReadString(File.DirApp,"test.txt")
make sure the file is there.
Seems like it is not?

Upload a small project showing the issue.

Away from that you should create a new thread for any new issue you have. Posting to existing threads is a mistake.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Try this file.
The content of your file exceeds the line limit.
You need to format the document
 

Attachments

  • test.txt
    353.6 KB · Views: 8
Upvote 0

Alpay ABAY

Member
Licensed User
Try this file.
The content of your file exceeds the line limit.
You need to format the document
As far as I know it is limited with memory, I parsed responses before a lot bigger.
The file u sent me works, but you made somehow prettier version (whyto?)

it is still not clear to me.
Test project is attached. Having same issue and original txt
 

Attachments

  • ParserTest.zip
    214.4 KB · Views: 9
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Works as expected if you use the parser right

B4X:
Private Sub Button1_Click
    Dim str As String = File.ReadString(File.DirAssets,"test.txt")
    Log(str)
    Dim parser As JSONParser
    parser.Initialize(str)
    Dim jRoot As Map = parser.NextObject
    Dim msg As String = jRoot.Get("msg")
    Log($"${msg}"$)

    Dim recipients As List = jRoot.Get("recipients")
    For Each colrecipients As Map In recipients
        Dim reskno As Int = colrecipients.Get("reskno")
        Dim surname As String = colrecipients.Get("surname")
        Dim num As String = colrecipients.Get("num")
        Dim name As String = colrecipients.Get("name")
        Dim title As String = colrecipients.Get("title")
        Log($"${reskno}:${num}:${name}:${title}"$)

    Next
    Dim id As String = jRoot.Get("id")
    Log($"${msg}"$)
    'Log(parser)
End Sub

Log(parser) isn´t expected to log anything useful in the LOG
 
Upvote 0

Alpay ABAY

Member
Licensed User
Ohh G.O.D
I understood, you check nextObject or nextArray, but it is not logical to log a string from side of parser after initialized correctlty,
which killed a lot of valuable time here.

Thanks DonManfred!
 
Upvote 0

Alpay ABAY

Member
Licensed User
it is not logical to log an OBJECT.

You can log the json you fill the parser with.
Agreed, not to agree. Javascript "console.log", php "var_dump", or "print_r" exist to check object content and status.
I understand b4x objects has default property as string for parser. I thought "Log" is having similar behavior, but obviously not. It is doing same for List and Maps, so you get they are not null, or having correct content.

If there is a better alternative command the "Log", I wish to know that.
Thank you anyway for your time and assistance.
 
Upvote 0
Top