B4J Question Json question regarding

giga

Well-Known Member
Licensed User
Back to JSON problem again. I cannot parse each result separately.
The original JSON file I receive is on one page.
What I would like to do is move down the page results by button to the next result
example result 0,1,2.


JSON file
{"page":1,"results":[{"id":510,"original_title":"One Flew Over the Cuckoo's Nest","release_date":"1975-11-18","title":"One Flew Over the Cuckoo's Nest","video":false,"vote_average":7.8,"vote_count":796},
{"id":328888,"original_title":"Tha Making of One Flew Over the Cuckoo's Nest","release_date":"2002-03-05","title":"Tha Making of One Flew Over the Cuckoo's Nest","video":false,"vote_average":0.0,"vote_count":0},
{"id":82386,"original_title":"Ranma ½: One Flew Over the Kuno's Nest","release_date":"1994-08-20","title":"Ranma ½: One Flew Over the Kuno's Nest","video":false,"vote_average":0.0,"vote_count":0}],"total_pages":1,"total_results":3}

This is parsed:
  • results
    • 0
      • id: 510
      • title: One Flew Over the Cuckoo's Nest
      • vote_average: 7.8
      • release_date: 1975-11-18
      • original_title: One Flew Over the Cuckoo's Nest
      • vote_count: 796
      • video: false
    • 1
      • id: 328888
      • title: Tha Making of One Flew Over the Cuckoo's Nest
      • vote_average: 0.0
      • release_date: 2002-03-05
      • original_title: Tha Making of One Flew Over the Cuckoo's Nest
      • vote_count: 0
      • video: false
    • 2
      • id: 82386
      • title: Ranma ½: One Flew Over the Kuno's Nest
      • vote_average: 0.0
      • release_date: 1994-08-20
      • original_title: Ranma ½: One Flew Over the Kuno's Nest
      • vote_count: 0
      • video: false
  • page: 1
  • total_pages: 1
  • total_results: 3

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim results As List = root.Get("results")
For Each colresults As Map In results
 Dim id As Int = colresults.Get("id")
 Dim title As String = colresults.Get("title")
 Dim vote_average As Double = colresults.Get("vote_average")
 Dim release_date As String = colresults.Get("release_date")
 Dim original_title As String = colresults.Get("original_title")
 Dim vote_count As Int = colresults.Get("vote_count")
 Dim video As String = colresults.Get("video")
Next
Dim page As Int = root.Get("page")
Dim total_pages As Int = root.Get("total_pages")
Dim total_results As Int = root.Get("total_results")
Any suggestions appreciated.

Thanks in advance.
 

inakigarm

Well-Known Member
Licensed User
Can you explain a little more where's the problem, for me it's ok ! (JSON file Ok, parser OK)

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    Dim str As String
   
    str=$"{"page":1,"results":[{"id":510,"original_title":"One Flew Over the Cuckoo's Nest","release_date":"1975-11-18","title":"One Flew Over the Cuckoo's Nest","video":False,"vote_average":7.8,"vote_count":796},
{"id":328888,"original_title":"Tha Making of One Flew Over the Cuckoo's Nest","release_date":"2002-03-05","title":"Tha Making of One Flew Over the Cuckoo's Nest","video":False,"vote_average":0.0,"vote_count":0},
{"id":82386,"original_title":"Ranma ½: One Flew Over the Kuno's Nest","release_date":"1994-08-20","title":"Ranma ½: One Flew Over the Kuno's Nest","video":False,"vote_average":0.0,"vote_count":0}],"total_pages":1,"total_results":3}"$
    Dim parser As JSONParser
    parser.Initialize(str)
    Dim root As Map = parser.NextObject
    Dim results As List = root.Get("results")
    For Each colresults As Map In results
     Dim id As Int = colresults.Get("id")
     Dim title As String = colresults.Get("title")
     Dim vote_average As Double = colresults.Get("vote_average")
     Dim release_date As String = colresults.Get("release_date")
     Dim original_title As String = colresults.Get("original_title")
     Dim vote_count As Int = colresults.Get("vote_count")
     Dim video As String = colresults.Get("video")
     Log (id)
     Log (title)
     Log (vote_average)
      Log (release_date)
     Log ( original_title)
     Log ( vote_count)
     Log ( video)
    
    Next
    Dim page As Int = root.Get("page")
    Dim total_pages As Int = root.Get("total_pages")
    Dim total_results As Int = root.Get("total_results")
   
End Sub
 

giga

Well-Known Member
Licensed User
Thanks for the reply. Yes it does parse but you will see in the example
3 TOTAL results are found:

Each result having these categories:
  • id:
  • title:
  • vote_average:
  • release_date:
  • original_title:
  • vote_count:
  • video:

What I am trying to do is use a button and 7 textboxes(for categories) to show its content (one result at a time)

Problem is I cannot move(iterate) through the parsed data by result number

button click (first result) should show
result 0 and its 7 categories

button click again (second result) should show
result 1 and its 7 categories

button click and again (third result) should show
results 2 and its 7 categories

Hope that makes it clearer.
 

Daestrum

Well-Known Member
Licensed User
Try
B4X:
    Dim parser As JSONParser
    parser.Initialize(str)
    Dim root As Map = parser.NextObject
    Dim results As List = root.Get("results")
    Dim resSize As Int = results.size
    Dim id(resSize),vote_count(resSize) As Int
    Dim vote_average(resSize) As Double
    Dim title(resSize), release_date(resSize), original_title(resSize),video(resSize) As String 
    Dim c As Int = 0
    For Each colresults As Map In results
     id(c) = colresults.Get("id")
     title(c) = colresults.Get("title")
     vote_average(c) = colresults.Get("vote_average")
     release_date(c) = colresults.Get("release_date")
     original_title(c) = colresults.Get("original_title")
     vote_count(c) = colresults.Get("vote_count")
     video(c) = colresults.Get("video")
    c = c + 1
    Next
    For a = 0 To results.Size -1
    Log("record: " & a)
        Log (id(a))
       Log (title(a))
       Log (vote_average(a))
       Log (release_date(a))
       Log (original_title(a))
       Log (vote_count(a))
       Log (video(a))
        Log("-----------------------" & CRLF)
    Next
Your results are now readable using id(1)...video(1) etc for result #2
 
Top