Android Question Quiz with json file

jchal

Active Member
Licensed User
Longtime User
Hi all
I am trying to make a Quiz with a json file
I managed to read the json file, my problem is how to add the results in the B4A page. I design a page with 2 labels and 5 buttons
The 1st label is question number 1 of 20 for example
2nd label is for question
The first 4 buttons are for the 4 answers.
The 5th button is for skip a question
In the json file you will see the correct answer as index number.

The main Idea is to display the answers one by one the user must answer the question and move to the next one.

Read Json:
Sub Activity_Create(FirstTime As Boolean)
    
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.bz.com/in/intQ.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 parser As JSONParser
    parser.Initialize( s )
'    parser.Initialize(File.ReadString(File.DirAssets, "intQ.json"))
    Dim jRoot As Map = parser.NextObject
    Dim questions As List = jRoot.Get("questions")
    For Each colquestions As Map In questions
        Dim question As String = colquestions.Get("question")
        Dim answers As List = colquestions.Get("answers")
        'Dim correctIndex As Int = colquestions.Get("correctIndex")
        For Each colanswers As String In answers
        Next
        Dim correctIndex As Int = colquestions.Get("correctIndex")
    Next
        
    
    Activity.LoadLayout("QuizL")
End Sub

The Json file - intQ.json:
{
"questions": [
{
"question": "What is the scientific name of a butterfly?",
"answers": [
"Apis",
"Coleoptera",
"Formicidae",
"Rhopalocera"
],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": [
"1,233 K",
"5,778 K",
"12,130 K",
"101,300 K"
],
"correctIndex": 1
},
{
"question": "Who are the actors in The Internship?",
"answers": [
"Ben Stiller, Jonah Hill",
"Courteney Cox, Matt LeBlanc",
"Kaley Cuoco, Jim Parsons",
"Vince Vaughn, Owen Wilson"
],
"correctIndex": 3
},
{
"question": "What is the capital of Spain?",
"answers": [
"Berlin",
"Buenos Aires",
"Madrid",
"San Juan"
],
"correctIndex": 2
},
{
"question": "What are the school colors of the University of Texas at Austin?",
"answers": [
"Black, Red",
"Blue, Orange",
"White, Burnt Orange",
"White, Old gold, Gold"
],
"correctIndex": 2
},
{
"question": "What is 70 degrees Fahrenheit in Celsius?",
"answers": [
"18.8889",
"20",
"21.1111",
"158"
],
"correctIndex": 2
},
{
"question": "When was Mahatma Gandhi born?",
"answers": [
"October 2, 1869",
"December 15, 1872",
"July 18, 1918",
"January 15, 1929"
],
"correctIndex": 0
},
{
"question": "How far is the moon from Earth?",
"answers": [
"7,918 miles (12,742 km)",
"86,881 miles (139,822 km)",
"238,400 miles (384,400 km)",
"35,980,000 miles (57,910,000 km)"
],
"correctIndex": 2
},
{
"question": "What is 65 times 52?",
"answers": [
"117",
"3120",
"3380",
"3520"
],
"correctIndex": 2
},
{
"question": "How tall is Mount Everest?",
"answers": [
"6,683 ft (2,037 m)",
"7,918 ft (2,413 m)",
"19,341 ft (5,895 m)",
"29,029 ft (8,847 m)"
],
"correctIndex": 3
},
{
"question": "When did The Avengers come out?",
"answers": [
"May 2, 2008",
"May 4, 2012",
"May 3, 2013",
"April 4, 2014"
],
"correctIndex": 1
},
{
"question": "What is 48,879 in hexidecimal?",
"answers": [
"0x18C1",
"0xBEEF",
"0xDEAD",
"0x12D591"
],
"correctIndex": 1
}
]
}
But I am stuck, I am uploading the code and the json file.
I appreciate if you can help.
 

Attachments

  • test.zip
    11.3 KB · Views: 244

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Try this.

I have hacked the 3 pages standard b4xpages example to do most of what you describe. It should get you started.

Cheers
 

Attachments

  • questions.zip
    192.7 KB · Views: 269
Upvote 1
Top