iOS Question [Solved] Help with json paras

Discussion in 'iOS Questions' started by Sasuke Sama, May 26, 2019.

  1. Sasuke Sama

    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
    Code:
    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: May 26, 2019
  2. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    I don't see any problem.

    Code:
    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")
     
  3. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    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
     
  4. Semen Matusovskiy

    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 [ ... ]

    Code:
    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.
     
  5. aaronk

    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.

    Code:
    [{"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: May 26, 2019
  6. mcqueccu

    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


    Code:
    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
     
  7. Sasuke Sama

    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 :)
     
  8. DonManfred

    DonManfred Expert Licensed User

    Code:
    Dim parser As JSONParser
    parser.Initialize(<text>)
    Dim root As Map = parser.NextObject
    Dim name As String = root.Get("name")
    Dim tm As String = root.Get("tm")
    Dim id As String = root.Get("id")
    Dim commants As String = root.Get("commants")
    works here
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice