Android Question How To Parse JSON

syerif

Active Member
Licensed User
Longtime User
Hi, I got a problem parsing JSON format

{
"success": true,
"data": [
{
"total": "15"
},
[
{
"rows": [
{
"id": 1,
"aton_type": 1,
"aton_name": "TANJUNG LAYAR",
"degree1": "-6",
"minute1": "45",
"second1": "0",
"direction1": "N",
"degree2": "105",
"minute2": "12",
"second2": "30",
"direction2": "E",
"location": "Banten",
"date_establish": "1879-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-09-25T07:17:35.325Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 2,
"aton_type": 1,
"aton_name": "CIKONENG",
"degree1": "-6",
"minute1": "4",
"second1": "0",
"direction1": "N",
"degree2": "105",
"minute2": "53",
"second2": "0",
"direction2": "E",
"location": "Banten",
"date_establish": "1883-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:12:09.105Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 3,
"aton_type": 1,
"aton_name": "PULAU TUNDA",
"degree1": "-5",
"minute1": "49",
"second1": "0",
"direction1": "N",
"degree2": "106",
"minute2": "17",
"second2": "0",
"direction2": "E",
"location": "Banten",
"date_establish": "1970-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:16:13.902Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 4,
"aton_type": 1,
"aton_name": "PULAU TEMPURUNG",
"degree1": "-5",
"minute1": "54",
"second1": "3",
"direction1": "N",
"degree2": "105",
"minute2": "56",
"second2": "0",
"direction2": "E",
"location": "Banten",
"date_establish": "1919-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:19:41.881Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 5,
"aton_type": 1,
"aton_name": "PULAU DELI",
"degree1": "-7",
"minute1": "1",
"second1": "0",
"direction1": "N",
"degree2": "105",
"minute2": "31",
"second2": "25",
"direction2": "E",
"location": "Banten",
"date_establish": "2006-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:21:26.339Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 6,
"aton_type": 3,
"aton_name": "CITUIS",
"degree1": "-6",
"minute1": "1",
"second1": "54.6",
"direction1": "N",
"degree2": "106",
"minute2": "34",
"second2": "7",
"direction2": "E",
"location": "Banten",
"date_establish": "2004-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:26:45.641Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 7,
"aton_type": 3,
"aton_name": "KARANG ANTU",
"degree1": "-6",
"minute1": "1",
"second1": "40",
"direction1": "N",
"degree2": "106",
"minute2": "9",
"second2": "40",
"direction2": "E",
"location": "Banten",
"date_establish": "1973-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:28:35.919Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 8,
"aton_type": 3,
"aton_name": "PULAU ULAR",
"degree1": "-6",
"minute1": "0",
"second1": "33",
"direction1": "N",
"degree2": "155",
"minute2": "55",
"second2": "34",
"direction2": "E",
"location": "Banten",
"date_establish": "1999-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:29:55.459Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 9,
"aton_type": 3,
"aton_name": "PULAU SANGIANG",
"degree1": "-5",
"minute1": "58",
"second1": "20",
"direction1": "N",
"degree2": "105",
"minute2": "51",
"second2": "08",
"direction2": "E",
"location": "Banten",
"date_establish": "1997-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:31:16.547Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 10,
"aton_type": 3,
"aton_name": "KARDINAL BARAT KARANG / TERUMBU KOLIOT",
"degree1": "-5",
"minute1": "55",
"second1": "18",
"direction1": "N",
"degree2": "105",
"minute2": "48",
"second2": "52.32",
"direction2": "E",
"location": "Banten",
"date_establish": "2017-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:33:00.298Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 11,
"aton_type": 2,
"aton_name": "MPMT TG PRIOK",
"degree1": "-6",
"minute1": "2",
"second1": "16.40",
"direction1": "N",
"degree2": "106",
"minute2": "53",
"second2": "2.795",
"direction2": "E",
"location": "Banten",
"date_establish": "2011-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:45:43.248Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 12,
"aton_type": 2,
"aton_name": "TG.PRIOK NO.1",
"degree1": "-6",
"minute1": "2",
"second1": "48",
"direction1": "N",
"degree2": "106",
"minute2": "52",
"second2": "37",
"direction2": "E",
"location": "DKI Jakarta",
"date_establish": "2015-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:45:49.800Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 13,
"aton_type": 2,
"aton_name": "TG.PRIOK NO.2",
"degree1": "-6",
"minute1": "2",
"second1": "48",
"direction1": "N",
"degree2": "106",
"minute2": "53",
"second2": "12",
"direction2": "E",
"location": "DKI Jakarta",
"date_establish": "2015-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:49:58.038Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 14,
"aton_type": 2,
"aton_name": "TG.PRIOK NO.3",
"degree1": "-6",
"minute1": "3",
"second1": "28.13",
"direction1": "N",
"degree2": "106",
"minute2": "52",
"second2": "53.07",
"direction2": "E",
"location": "DKI Jakarta",
"date_establish": "1995-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:51:41.773Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
},
{
"id": 15,
"aton_type": 2,
"aton_name": "TG.PRIOK NO.4",
"degree1": "-6",
"minute1": "3",
"second1": "28.61",
"direction1": "N",
"degree2": "106",
"minute2": "53",
"second2": "2.81",
"direction2": "E",
"location": "DKI Jakarta",
"date_establish": "1995-01-01T00:00:00.000Z",
"owner": "Disnav Priok",
"remarks": null,
"picture": null,
"created_at": "2021-10-13T03:53:43.130Z",
"updated_at": null,
"deleted_at": null,
"deleted_by": null,
"updated_by": null,
"created_by": null,
"is_delete": false,
"djpl": true,
"radius": 1000
}
]
}
]
]
}

i have to try JSON Tree and got an error when testing the generated code

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim data As List = root.Get("data")
For Each coldata As Map In data
 Dim total As String = coldata.Get("total")
Next
Dim success As String = root.Get("success")
 

jahswant

Well-Known Member
Licensed User
Longtime User
Check this !

 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i use a lot of REST api's, and, as a result, i see a lot of
json strings. i have no problem parsing them with our
json parser. until today. the OP's example is very
interesting, and normal processing will generate java
errors.

i can parse the string, but it's very ugly. and while i
do not normally doubt my own coding, there is
something embarrassing about what i had to do to
parse this string. not quite failing a smell test, but
something seems forced. i hope our json mavens
can see how to make this work using our normal
processing.

the issue i ran across relates to the so-called "data"
list. if you look at it, you can see that odd-numbered
items in the list are maps, while even-numbered items
are, themselves, lists. i don't think we have a way of
dealing with that, even though our list object can
accept any type of object. i tried casting items as
different types, but that threw exceptions. so i
ended up reading from the list one way for even-numbered
items and another way for odd-numbered items. it
felt strange, but it worked. i used log entries to keep track
of what the parser was seeing. i removed some to maintain
focus. i also truncated the OP's string. only a couple items
are necessary to parse the thing.

i posted the OP's string at 2 online json parser sites, and
they both stepped through it without issue, dispelling
any thoughts i may have had about its being malformed.

in any case, here's how i did it (and the result). i look
forward to someone's more succinct version.

B4X:
    Dim json, liljson As JSONParser
    json.Initialize( text )
    
    Dim root As Map = json.NextObject
    Dim data As List = root.Get( "data" )
    Log("items: " & data.Size)
    
    Dim i As Int
    For i = 0 To data.Size - 1
        Log("looking at item: " & i)
        Dim theitem As String = data.Get(i)
        Log("theitem: " & theitem)
        
        If i Mod data.Size = 0 Then  ' odd items are a simple map
            Dim entry As Map = data.Get(i)
            Dim total As String = entry.Get("total")
            Log("total:" & total)
        Else           ' even items are a list
            Dim lillist As List = data.Get(i)
            Dim j As Int
            For j = 0 To lillist.Size - 1
                Dim lilmap As Map = lillist.Get(j)
                Dim list2 As List = lilmap.Get("rows")
                Dim k As Int
                For k = 0 To list2.Size - 1
                    Dim lilmap2 As Map = list2.Get(k)
                    Log(lilmap2.Get("aton_name"))
                Next
            Next
        End If
    Next
    Log("success: " & root.Get("success"))
 

Attachments

  • Capture.png
    Capture.png
    83.7 KB · Views: 244
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Other:

B4X:
    Dim parser As JSONParser
    parser.Initialize(sText)
    Dim mRoot As Map = parser.NextObject
    Dim data As List = mRoot.Get("data")
    
    Dim success As String = mRoot.Get("success")
    Log(success)
    
    Dim total As String = data.Get(0).As(Map).Get("total")
    Log(total)
    
    Dim rows  As List = data.Get(1).As(List).Get(0).As(Map).Get("rows")
    For Each coldata As Map In rows
        Dim id As String = coldata.Get("id")
        Dim aton_name As String = coldata.Get("aton_name")
        Log(id & " - "&  aton_name)
    Next

1635123103142.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
other: use "As"


sText: It is a string that contains the JSON

'ignore: is warning #22

B4X:
    Dim mRoot As Map = sText.As(JSON).ToMap 'ignore
    Dim data As List = mRoot.Get("data")

    Dim success As String = mRoot.Get("success")
    Log(success)
 
    Dim total As String = data.Get(0).As(Map).Get("total")
    Log(total)
 
    Dim rows  As List = data.Get(1).As(List).Get(0).As(Map).Get("rows")
    For Each coldata As Map In rows
        Dim id As String = coldata.Get("id")
        Dim aton_name As String = coldata.Get("aton_name")
        Log(id & " - "&  aton_name)
    Next

1635124179399.png
 
Upvote 0

syerif

Active Member
Licensed User
Longtime User
Other:

B4X:
    Dim parser As JSONParser
    parser.Initialize(sText)
    Dim mRoot As Map = parser.NextObject
    Dim data As List = mRoot.Get("data")
    
    Dim success As String = mRoot.Get("success")
    Log(success)
    
    Dim total As String = data.Get(0).As(Map).Get("total")
    Log(total)
    
    Dim rows  As List = data.Get(1).As(List).Get(0).As(Map).Get("rows")
    For Each coldata As Map In rows
        Dim id As String = coldata.Get("id")
        Dim aton_name As String = coldata.Get("aton_name")
        Log(id & " - "&  aton_name)
    Next

View attachment 120739
You are the best, thank you...
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
orher: ????

direct access to the structure and less code to use

B4X:
    Dim mRoot As Map = sText.As(JSON).ToMap 'ignore

    Dim success As String = mRoot.Get("success")
    Log(success)
    
    Dim total As String = mRoot.Get("data").As(List).Get(0).As(Map).Get("total")
    Log(total)
    
    For Each coldata As Map In  mRoot.Get("data").As(List).Get(1).As(List).Get(0).As(Map).Get("rows").As(List)
        Dim id As String = coldata.Get("id")
        Dim aton_name As String = coldata.Get("aton_name")
        Log(id & " - "&  aton_name)
    Next
 
Upvote 1
Top