JSON parsing generates Unterminated Object error

Inman

Well-Known Member
Licensed User
Longtime User
I am trying to parse a long JSON string with children, sub children, sub sub children etc...So I thought of doing it the recursive way.

Now JSON Parser excepts the string in the following format:

B4X:
[{"kind": "Listing", "data": {"modhash": "", "children": [{"kind": "t3", "data": .....................

I read the downloaded file using File.ReadString and it does return the string in the above format.

Then I do this:

B4X:
JSON.Initialize(File.ReadString(File.DirInternalCache, "main.json"))
   
   Dim m As Map 'helper map for navigating
   Dim MenuItems, ArrayItems As List, domain,url,surl As String, i As Int
   
   ArrayItems = JSON.NextArray

   For i=1 To ArrayItems.Size-1
      parseJSON (ArrayItems.Get(i))      
   Next

Now what happens is that ArrayItems.Get(i) returns the string, but without all the necessary double quotes. That is the string

B4X:
{"data"={"after"=null, "children"=[{"data"={"edited"="false", "body"="man committing crimes seems so much easier back in the day." ,

becomes

B4X:
{data={after=null, children=[{data={edited=false, body=man committing crimes seems so much easier back in the day. ,

As a result, even before the recursion starts, I get this error:

org.json.JSONException: Unterminated object at character 60 of {data={after=null, children=[{data={edited=false, body=man committing crimes seems so much easier back in the day. , author_flair_css_class=null, body_html=<div class="md"><p>man committing crimes seems so much easier back in the day. </p>

Character 60 is the space between man and committing. Is this an issue of the JSON parser? How can I successfully continue parsing?
 

Inman

Well-Known Member
Licensed User
Longtime User
Ok. So is it possible to recursively parse the whole JSON file and keep moving to NextObject and NextArray without calling JSON.initialise each time?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So is it possible to recursively parse the whole JSON file and keep moving to NextObject and NextArray without calling JSON.initialise each time?
Not sure what you mean with "recursively parse". You should call Json.Initialize only once. Json.NextObject will return a List that holds Maps that hold...
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Not sure what you mean with "recursively parse". You should call Json.Initialize only once. Json.NextObject will return a List that holds Maps that hold...

You are right. JSON parsing needs to be done only once. The recursion should then be done separately with another function using the output of JSON.NextObject each time. I was able to implement that. Thank you.
 
Upvote 0
Top