Android Question Parsing json from url question

Mick96

Member
Licensed User
Hello. I have a json file on an http site and I'm struggling to understand the process.
After going to: https://b4x.com:51041/json/index.html and letting B4X generate the code. I'm trying to understand what value I need in line 2 where you see the (<text>)
object. I can't figure this out. The json file is at http://www.oblongapps.com/oblongapp/mainmenu.json
The json parses perfectly in objective c in Xcode but I'm unfamiliar with how B4X is getting the data.
What do I do with the (<text>) ?
Thanks!!

Below is the output from the code generator:

Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim menu As List = root.Get("menu")
For Each colmenu As Map In menu
Dim image As String = colmenu.Get("image")
Dim detailText As String = colmenu.Get("detailText")
Dim link As String = colmenu.Get("link")
Dim text As String = colmenu.Get("text")
Dim content As String = colmenu.Get("content")
Next
 

drgottjr

Expert
Licensed User
Longtime User
here's the code (todo: some error checking and or try/catch blocks is always helpful when pulling down stuff from the wild):
B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("http://www.oblongapps.com/oblongapp/mainmenu.json")
   
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim s As String = j.getstring
        Log(s)
    Else
        Log(LastException)
    End If
    j.Release
   
    Dim json As JSONParser
    json.Initialize( s )
    Dim walker As Map
    walker = json.NextObject
    Dim menulist As List = walker.Get("menu")
    Dim i As Int
    For i = 0 To menulist.Size - 1
        Dim menuitem As Map = menulist.Get(i)
       
        Dim text As String = menuitem.Get("text")
        Dim detailtext As String = menuitem.Get("detailText")
        Dim link As String = menuitem.Get("link")
        Dim image As String = menuitem.Get("image")
        Dim content As String = menuitem.Get("content")
        Log($"${text} ${detailtext} ${link} ${image} ${content}"$)  
    Next

a partial snapshot of the output is attached.

if you want the image and the content, those are separate operations; you'll have to go back and get them.
 

Attachments

  • output.png
    output.png
    17.4 KB · Views: 207
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
wait, sorry, wrong snapshot. what i posted previously was the input. here's the output:
 

Attachments

  • output2.PNG
    output2.PNG
    22.6 KB · Views: 192
Upvote 0

Mick96

Member
Licensed User
AWESOME!! That output exactly what i wanted. Now I'm going to plow through your code and see how that works.
I don't understand it..... but I will. Thanks so Much!!
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
now that i look at your code, i think you basically had it, except for outputting the result. that and indenting. indenting gives you a sense of hierarchy. things are not as linear as you painted them. carry on.
 
Upvote 0

Mick96

Member
Licensed User
@Mick96 years ago Anywhere Software created an online B4X source code generator for parsing JSON data. Anywhere Software also released a .jar file that also generates B4X source code automatically from JSON data.

Clink on the link below.
Free JSON parsing tools...
Yes, Thanks, That's actually what I used to generate the code but the generated code contained a (<text>) and I didn't understand what value it needed.
You guys are Great!! Thanks for all the Help and suggestions.
 
Upvote 0

Mick96

Member
Licensed User
now that i look at your code, i think you basically had it, except for outputting the result. that and indenting. indenting gives you a sense of hierarchy. things are not as linear as you painted them. carry on.
Thanks, I'm an old Objective C guy and am working on an app that will need an Android version. B4A Looks like he best way to get this done but boy do I have a lot of learning to do. This reminds me of the VB5 i used 20 years ago. Hopefully I will catch on. Thanks so Much!!
 
Upvote 0
Top