Android Question parser.NextObject error End of input at character 0 of

angel_

Well-Known Member
Licensed User
Longtime User
Through Crashlytics I get the following error from one of my apps

Crashlytics:
JSONParser.java line 48
anywheresoftware.b4a.objects.collections.JSONParser.NextObject
Fatal Exception: org.json.JSONException
End of input at character 0 of

Code:
Dim parser As JSONParser
parser.Initialize(FileText)
Dim Map1 As Map = parser.NextObject '<-- Error in this line according to Crashlytics

I understand that the problem (End of input at character 0 of) occurs because the JSON file was not created correctly, this JSON I generate with this event

B4XMainPage:
Sub B4XPage_Background
    ...
    Private JSONGenerator As JSONGenerator

    JSONGenerator.Initialize(MapaDatos)

    JS = JSONGenerator.ToPrettyString(2)

    File.WriteString(Path, FileText, JS)
End Sub

Should I use a Try...Catch before ending B4XPage_Background or the problem is another
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
Is the json you want to parse a EMPTY String???

If not: post the json
In principle it should never be empty since I load the JSON and then I create the file, it is not possible to save the file if there is no JSON data and then before opening the JSON I verify that the file exists

JSON:
{
  "lista": [
    {
      "id": "p_base",
      "lista": [
        224,
        224,
        2912,
        2576
      ]
    },
    {
      "id": "p_cont",
      "lista": [
        224,
        224,
        0,
        0,
        0
      ]
    },
    {
      "id": "p_cont_tag",
      "lista": [
        1,
        "",
        "horizontal",
        "",
        112,
        0,
        "center_left"
      ]
    },
    {
      "id": "p_mov",
      "lista": [
        224,
        224,
        0,
        0
      ]
    },
    {
      "id": "img",
      "lista": [
        224,
        224,
        0,
        0
      ]
    },
    {
      "id": "l_info",
      "lista": [
        224,
        0,
        0,
        188
      ]
    },
    {
      "id": "p_base",
      "lista": [
        224,
        224,
        3024,
        2464
      ]
    },
    {
      "id": "p_cont",
      "lista": [
        224,
        224,
        0,
        0,
        0
      ]
    },
    {
      "id": "p_cont_tag",
      "lista": [
        1,
        "",
        "vertical",
        "",
        112,
        0,
        "center_left"
      ]
    },
    {
      "id": "p_mov",
      "lista": [
        224,
        224,
        0,
        0
      ]
    },
    {
      "id": "img",
      "lista": [
        224,
        224,
        0,
        0
      ]
    },
    {
      "id": "l_info",
      "lista": [
        224,
        0,
        0,
        188
      ]
    }
  ]
}
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
This is how I generate the JSON

B4X:
For Each v As B4XView In pnl.GetAllViewsRecursive
    ...

    Private JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(MapaDatos)
    JS = JSONGenerator.ToPrettyString(2)
        
    File.WriteString(Folder, FileName, JS)
Next

Maybe I should put the last line (File.WriteString...) outside of For Each... Next, like this:
B4X:
Private JSONGenerator As JSONGenerator
Dim TextJS As String = ""
For Each v As B4XView In pnl.GetAllViewsRecursive
    ...
    
    JSONGenerator.Initialize(MapaDatos)
    JS = JSONGenerator.ToPrettyString(2)
Next
File.WriteString(Folder, FileName, JS)
 
Last edited:
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
I think I don't explain me well, this is the code:

now:
Dim JS As String
Dim MapaDatos As Map
Dim LstMapa As List

MapaDatos.Initialize
LstMapa.Initialize

For Each v As B4XView In pnl.GetAllViewsRecursive
    '...code
    '... load LstMapa
    
    MapaDatos.Put("lista", LstMapa)
    Private JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(MapaDatos)
    JS = JSONGenerator.ToPrettyString(2)
        
    File.WriteString(Folder, FileName, JS)
Next

I can File.WriteString move after the loop, like this?
B4X:
Dim JS As String
Dim MapaDatos As Map
Dim LstMapa As List

MapaDatos.Initialize
LstMapa.Initialize

For Each v As B4XView In pnl.GetAllViewsRecursive
    '...code
    '... load LstMapa
    
    MapaDatos.Put("lista", LstMapa)
    Private JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(MapaDatos)
    JS = JSONGenerator.ToPrettyString(2)
Next
File.WriteString(Folder, FileName, JS) ' <-- AFTER THE LOOP

The other question is, Should I include a Wait for in B4XPage_Background as suggested by aeric
 
Upvote 0
Top