Android Question JSON Parsing problems

FrankBerra

Active Member
Licensed User
Longtime User
Hi all i am facing a problem with parsing JSON with spaces in strings

This is my the JSON i am trying to parse:
B4X:
{
        "523D837E66FF3EA3FE892ACFECA6112112984710068A03A367C22D04C8618242": "Colleghi",
        "1705C6D36D0987B429A32DCF5A174339F9FD15B5E5011D2FB662FDAB7B2504D4": "Familiari2",
        "A06D3D4AFC9D233276FBD6969C625B77CD6EEA103346B1B6D575C07C8C523D72": "Amici2",
        "41944DF4B2A83B5A85F14DFFA6F8212A3373B4E141B00F2489B4974F36EE8A1B": "This is a test"
    }

I used this tool http://basic4ppc.com:51042/json/index.html
and the code i use for parsing is this:

B4X:
Dim DecoderNomiRealiListe As JSONParser
DecoderNomiRealiListe.Initialize(NomiRealiListe)
Dim NomiRealiListeDecodificate As Map = DecoderNomiRealiListe.NextObject

I obtain an error saying:
B4X:
org.json.JSONException: Unterminated object at character 297 of {523D837E66FF3EA3FE892ACFECA6112112984710068A03A367C22D04C8618242=Colleghi, 1705C6D36D0987B429A32DCF5A174339F9FD15B5E5011D2FB662FDAB7B2504D4=Familiari2, A06D3D4AFC9D233276FBD6969C625B77CD6EEA103346B1B6D575C07C8C523D72=Amici2, 41944DF4B2A83B5A85F14DFFA6F8212A3373B4E141B00F2489B4974F36EE8A1B=This is a test}

The char at position 297 corresponds at the space in the string.
The error seems to happen because the parser removes the quetes from the JSON string.

How can i avoid this error?

Thanks in advance
 

eps

Expert
Licensed User
Longtime User
Hmm... I think the space is not the issue - there's something else that the parser doesn't like with the input or possibly the way it is processing/getting it.

What version of the JSON library are you using?

How are you parsing the string?

Have you debugged the code up to the point where the error occurs?
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
B4X:
Sub JobDone (Job As HttpJob)
If Job.Success Then
    Dim res As String
        res = Job.GetString
        res = "[" & res & "]"   '<----- Add this
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
......

OR ( better ) in your file php check that you have []
B4X:
$today[] = array ('oggi'=>date("n/j/Y")); // <----- []
print json_encode($today);
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
The JSON in my first post was just a portion of a bigger and more complex JSON object.
After debugging some hours i understood that i was parsing the whole JSON object in a wrong way, even if was everything working for months!
I solved everything using the super "debugging" tool: http://basic4ppc.com:51042/json/index.html

Anyway thank you guys for your support! :)
 
Upvote 0
Top