Android Question (JSONException) org.json.JSONException: Unterminated object

Addo

Well-Known Member
Licensed User
hello everyone , i am trying to use jsonparser to parse the following jsonstring


B4X:
Dim pjson As JSONParser

Dim sparticpants As String = "[{display=MA . ., setup=false, muted=false}]"
pjson.Initialize(sparticpants)

I got this exception

(JSONException) org.json.JSONException: Unterminated object at character 14 of [{display=MA . ., setup=false, muted=false}]
i couldnt figure out what could be my mistake ?
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't know much about JSON, but when I put your JSON text into an online JSON parser then I get the error message that you describe.

However, if I put this JSON object into the parser there are no errors :
JSON:
[{"display":"MA . .", "setup":false, "muted":false}]

I hope that is of some help.
 
Upvote 0

Addo

Well-Known Member
Licensed User
I don't know much about JSON, but when I put your JSON text into an online JSON parser then I get the error message that you describe.

However, if I put this JSON object into the parser there are no errors :
JSON:
[{"display":"MA . .", "setup":false, "muted":false}]

I hope that is of some help.
this error will show up if you start to literate through the parsed record

B4X:
Dim pjson As JSONParser
Dim jsonstr As String = "[{display=MA . ., setup=false, muted=false}]"
Dim sparticpants As String = jsonstr
pjson.Initialize(sparticpants)
   
If pjson <> Null Then
        If pjson.IsInitialized Then
            Dim MuserList As List
            MuserList = pjson.NextArray
            If MuserList.Size > 0 Then
                Log(MuserList)
            End If
        End If
End If
 
Upvote 0

jahswant

Well-Known Member
Licensed User
Longtime User
Your json string seems not to be correct. Are you generating it yourself ?
Try something like this :
B4X:
[{"display"="MA . .", "setup"="false", "muted"="false"}]

I use this App http://www.b4x.com:51042/json/index.html

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As List = parser.NextArray
For Each coljRoot As Map In jRoot
 Dim display As String = coljRoot.Get("display")
 Dim setup As String = coljRoot.Get("setup")
 Dim muted As String = coljRoot.Get("muted")
Next
 
Upvote 0

Addo

Well-Known Member
Licensed User
Your json string seems not to be correct. Are you generating it yourself ?
Try something like this :
B4X:
[{"display"="MA . .", "setup"="false", "muted"="false"}]

I use this App http://www.b4x.com:51042/json/index.html

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As List = parser.NextArray
For Each coljRoot As Map In jRoot
 Dim display As String = coljRoot.Get("display")
 Dim setup As String = coljRoot.Get("setup")
 Dim muted As String = coljRoot.Get("muted")
Next
this string came from a backend that i am connected to i guess i have to double check that
 
Upvote 0

Addo

Well-Known Member
Licensed User
Your json string seems not to be correct. Are you generating it yourself ?
Try something like this :
B4X:
[{"display"="MA . .", "setup"="false", "muted"="false"}]

I use this App http://www.b4x.com:51042/json/index.html

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As List = parser.NextArray
For Each coljRoot As Map In jRoot
 Dim display As String = coljRoot.Get("display")
 Dim setup As String = coljRoot.Get("setup")
 Dim muted As String = coljRoot.Get("muted")
Next
when a json converted to a string its formated that way without cutations .. i dont think its from the backend , i guess display name has a invisible characters that cause this error if you change the display name to anything else it will work

example

B4X:
Dim pjson As JSONParser
Dim jsonstr As String = "[{display=anything, setup=false, muted=false}]"
Dim sparticpants As String = jsonstr
pjson.Initialize(sparticpants)
 
If pjson <> Null Then
        If pjson.IsInitialized Then
            Dim MuserList As List
            MuserList = pjson.NextArray
            If MuserList.Size > 0 Then
                Log(MuserList)
            End If
        End If
End If
 
Upvote 0

Addo

Well-Known Member
Licensed User
a dirty solution is to remove spaces from the json incomming string before parsing it

example

B4X:
sparticpants = sparticpants.Replace(" ", "")
sparticpants = sparticpants.Replace(CRLF,"")

not the best but it works fine now. thank you everyone
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
your "jsonstring" is not a json string or object. it was a jason object before having been
subjected to "toString()" and then returned it to you from some processing method.
many, many java objects can be seen in a string form with the "toString()" method.
in the case of a map (json object), "="'s replace ":"'s.

your first hint is the use of "=" instead of ":". that is the default stringifying of a
map. the second hint is the lack of strings as keys. this is a json requirement.
(values need only be valid json types). exhange ":" for "=" and put quotes around all
the keys.

so, the various fiddlings suggested above, will get you to where you need to be, but
you need to recognize when something is a valid json string or object. json has rules.
 
Upvote 0
Top