iOS Question [Solved] Help with json paras

Sasuke Sama

Active Member
Licensed User
Hi i have this json respond
https://snoanime.com/api-c/2938/data.php
im trying to get the data from it
i need the name
comment
tm
but json tree only reads the first value for some reason and whenever i try to paras it im getting
B4X:
Error parsing string: Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}
im still learning in json so any help it welcomed :D
thanx
 
Last edited:

Semen Matusovskiy

Well-Known Member
Licensed User
I don't see any problem.

B4X:
    Dim j                                                                       As HttpJob
    Dim jsonparserJSONParser                                                    As JSONParser
    Dim mapJSON                                                                 As Map
    Dim stringJSON                                                              As String
    Dim stringResult                                                            As String
   
    j.Initialize ("", Me)
    j.Download ("https://snoanime.com/api-c/3155/data.php")
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then stringJSON = j.GetString       
    j.Release
   
    jsonparserJSONParser.Initialize (stringJSON)
    mapJSON = jsonparserJSONParser.NextObject
    For Each stringKey As String In mapJSON.Keys
        stringResult = stringResult & stringKey & " = " & mapJSON.Get (stringKey) & CRLF
    Next
    Msgbox (stringResult, "Result")
 

Sasuke Sama

Active Member
Licensed User
I don't see any problem.

B4X:
    Dim j                                                                       As HttpJob
    Dim jsonparserJSONParser                                                    As JSONParser
    Dim mapJSON                                                                 As Map
    Dim stringJSON                                                              As String
    Dim stringResult                                                            As String
  
    j.Initialize ("", Me)
    j.Download ("https://snoanime.com/api-c/3155/data.php")
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then stringJSON = j.GetString      
    j.Release
  
    jsonparserJSONParser.Initialize (stringJSON)
    mapJSON = jsonparserJSONParser.NextObject
    For Each stringKey As String In mapJSON.Keys
        stringResult = stringResult & stringKey & " = " & mapJSON.Get (stringKey) & CRLF
    Next
    Msgbox (stringResult, "Result")
Thanks for answering looks like a posted the wrong link this one works fine but this dosent

https://snoanime.com/api-c/2938/data.php
 

Semen Matusovskiy

Well-Known Member
Licensed User
Well, this is simply not a correct JSON. Looks like array of JSONs, but in this case it's necessary to put in [ ... ]

B4X:
    Dim j                                                                       As HttpJob
    Dim jsonparserJSONParser                                                    As JSONParser
    Dim listJSON                                                                As List
    Dim mapJSON                                                                 As Map   
    Dim stringJSON                                                              As String   
   
    j.Initialize ("", Me)
    j.Download ("https://snoanime.com/api-c/2938/data.php")
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then stringJSON = j.GetString       
    j.Release
   
    stringJSON = "[" & stringJSON & "]"
    jsonparserJSONParser.Initialize (stringJSON)   
    listJSON = jsonparserJSONParser.NextArray   
    For i = 0 To listJSON.Size - 1
        Log ("---------------------------------------------------")       
        mapJSON = listJSON.Get (i)               
        For Each stringKey As String In mapJSON.Keys
            Log (stringKey & " = " & mapJSON.Get (stringKey))
        Next       
    Next
Results are in Log.
 

aaronk

Well-Known Member
Licensed User
Good tool to phrase the JSON:
http://www.basic4ppc.com:51042/json/index.html

Also, your JSON is most likely wrong.

Try putting a [ at the start of the JSON and a ] at the end of the JSON.

B4X:
[{"name":"Môhàmmëd Ğàsím","tm":"2019-05-19T14:08:09","id":"2938","commants":"hi. First comment"},{"name":"hero","commants":"❤","tm":"2019-05-20T19:45:24","id":""},{"name":"Jë M\u0027åppèłłė Åbđøů","tm":"2019-05-23T14:17:50","id":"2938","commants":"لم يعد هناك تحديد يظهر كل الحلقات التي شاهدت لكن التطبيق روعة"}]
 
Last edited:

mcqueccu

Active Member
Licensed User
As aaronk said, your JSON needs to be Array put [] at the beginning and at the end then you can parse it with this code


B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim name As String = colroot.Get("name")
 Dim tm As String = colroot.Get("tm")
 Dim id As String = colroot.Get("id")
 Dim commants As String = colroot.Get("commants")

'For testing purposes
log(name)
log(tm)
log(id)
log(commants)
Next
 

Sasuke Sama

Active Member
Licensed User
The json isnt mine and im not quite good at it so didnt get that
thank you everyone it works now :)
 

DonManfred

Expert
Licensed User
Top