Android Question JSON Expecting Array

barx

Well-Known Member
Licensed User
Good morning all

I'm dipping my toe in JSON and I'm struggling with something.

From what I have read this would be a valid JSON Array
{"north_face":"North Face","south_face":"South Face"}
but if I put this into JSON parser with
B4X:
    JSON.Initialize("{"north_face":"North Face","south_face":"South Face"}") 'it is actually fetched from online, but this simplifies it all
    JList = JSON.NextArray
I get an error telling me that a JSON Array is expected...

If I wrap the string with "[" "]" thus making it an Object, it seems to work... Am I loosing my Mind?
Can I just access the array straight off?

Maybe I'm formatting it wrong in the first place.

Here is the full version if it helps - The data is just random reall and doesn't mean much

[
{
"id": 1,
"client_id": 1,
"slug": "ghs-peterborough",
"project_reference": "GHS-Peterborough",
"description": "CrossKeys Homes social housing install.",
"area": "peterborough",
"photos_required": "{"generation_meter":"Generation Meter"}",
"allow_design": 0,
"active": 1,
"deleted_at": null,
"created_at": "2015-05-01 08:57:40",
"updated_at": "2015-05-01 08:57:40"
},
{
"id": 2,
"client_id": 2,
"slug": "wd-leeds",
"project_reference": "WD-Leeds",
"description": "Leeds City Council social housing install.",
"area": "Leeds",
"photos_required": "{"north_face":"North Face","south_face":"South Face"}",
"allow_design": 1,
"active": 1,
"deleted_at": null,
"created_at": "2015-05-01 08:57:40",
"updated_at": "2015-05-01 08:57:40"
}
]
And the code I have been playing with to log the info...

B4X:
Sub JobDone(Job As HttpJob)
   
    Dim JSON As JSONParser
    Dim JList As List
    JList.Initialize
   
    Log(Job.Getstring)
    JSON.Initialize(Job.GetString)
    JList = JSON.NextArray
   
    For Each item As Object In JList
        Log(item)
        Dim m As Map
        m.Initialize
        m = item
        For i = 0 To m.Size - 1
            Log(m.GetKeyAt(i) & " = " & m.GetValueAt(i))
        Next
       
        'JSON.Initialize(m.Get("photos_required")) '<-- No worky
        JSON.Initialize("[" & m.Get("photos_required") & "]") '<-- worky
        JList = JSON.NextArray
        m = JList.Get(0)
   
        For i = 0 To m.Size - 1
            Log("Photo " & i & ": " & m.GetValueAt(i))
       
        Next
       
    Next
End Sub
 
Top