iOS Question Error in Parsing JSON (list in a map)

Discussion in 'iOS Questions' started by hatzisn, Jun 1, 2019.

  1. hatzisn

    hatzisn Active Member Licensed User

    Hi everyone,

    consider the following JSON:

    Code:
    {"root":[{"VersionID":18,"SelectedVersion":1},{"VersionID":20,"SelectedVersion":2}]}
    This JSON cannot be parsed in B4i v5.50.
    The first step is concluded successfully but if you try to get the list you 're toasted for good (check the attached project). Any ideas anyone?

    Thanks in advance
     

    Attached Files:

  2. Sasuke Sama

    Sasuke Sama Active Member Licensed User

    Not a json expert but i think the formatting of the json is wrong
     
  3. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    Looks like correct JSON and works without problems
    Code:
    Dim sJSON As String = $"{"root":[{"VersionID":18,"SelectedVersion":1},{"VersionID":20,"SelectedVersion":2}]}"$
       
        
    Dim parser As JSONParser
        parser.Initialize (sJSON)   
        
    Dim map As Map = parser.NextObject
        
    Dim list As List = map.Get("root")
        
    For Each colroot As Map In list
            
    Log ("SelectedVersion = " & colroot.Get ("SelectedVersion"))
            
    Log ("VersionID = " & colroot.Get ("VersionID"))
        
    Next
     
  4. tufanv

    tufanv Expert Licensed User

  5. hatzisn

    hatzisn Active Member Licensed User

    I do not understand. The code by Semen works with me too. My code in the attached project does not work in b4i but works perfect in b4a as it is copied from the same project in b4a which works.
     
  6. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    Nothing strange. Your assign List to String in
    Code:
    sJSON = m.Get("root")
    Very doubt that there is a standard for similar conversion. Anyway, no reason to expect the same results in B4I and in B4A.

    BTW, your code is very redundant. For example, your initialize map/list and then get it from the parser.
    And to initialize parser one time is enough.
     
  7. hatzisn

    hatzisn Active Member Licensed User

    I don't really consider the code redundant. That is called defensive programming. My moto is "throw and handle exceptions only when you need them".
     
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