Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Cannot access the downloaded JSON
Started by smasherzz Mar 11 2019 01:18 AM

- - - - -
3 replies to this topic
json corona
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

smasherzz

[GLOBAL: userInfoPane.html]
smasherzz
  • Observer

  • 26 posts
  • Corona SDK

I have a JSON that I am downloading from the internet. When I print the JSON using: 

data = json.decode(event.response)
print(json.prettify(data))
 

It gets printed as shown below

[{
                        "description: ":"This is the quiz Description. ",
                        "day":"05",
                        "quiz_id":13,
                        "image":"http://shektech.org/quizapp/2019/03/basket123.png",
                        "year":"2019",
                        "theme":"first quiz",
                        "month":"03"
                      },{
                        "Question 1":{
                          "type":"multi",
                          "anstext":"This is the answer statement for questions 1 ",
                          "correct":"1",
                          "ansimage":"http://shektech.org/quizapp/2019/03/backicon.png",
                          "qheading":"Question No 1",
                          "questtext":"Answer: 1: Question Number 1 Statement: ",
                          "qimage":"http://shektech.org/quizapp/2019/03/B_1-1.png",
                          "option1":"This is option 1",
                          "hint":"The answer is 1.",
                          "option4":"This is Option 4",
                          "option2":"This is Option 2",
                          "option3":"This is Option 3"
                        }
                      },{
                        "Question 2":{
                          "type":"multi",
                          "anstext":"Question 2 Answer Statement:  ",
                          "correct":"4",
                          "ansimage":"http://shektech.org/quizapp/2019/03/basket123.png",
                          "qheading":"Question Number 2",
                          "questtext":"Answer: 4: Statement Questions Number 1: ",
                          "qimage":"http://shektech.org/quizapp/2019/03/B_2.png",
                          "option1":"This is Option 1",
                          "hint":"The answer is option 4",
                          "option4":"This is Option 4",
                          "option2":"This is Option 2",
                          "option3":"This is Option 3"
                        }
                      }]

I have trouble retrieving the values in the code. 

 

I tried the following code but it does not return the corresponding values:

print(data[0].quiz_id)

Is there something wrong with the JSON or am I not accessing it correctly in a table.



[TOPIC: post.html]
#2

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 743 posts
  • Corona SDK

Tables start at position 1 in lua, not 0.

 

Try changing 0 to 1.

 

print(data[1].quiz_id)


  • smasherzz likes this

[TOPIC: post.html]
#3

smasherzz

[GLOBAL: userInfoPane.html]
smasherzz
  • Observer

  • 26 posts
  • Corona SDK

Thanks. I took help from the link "https://stackoverflow.com/questions/42139363/how-to-parse-json-in-lua" and solved it.

                data = json.decode(event.response)
                quizObject = json.encode(data)
                quizObject = string.sub(quizObject, 2, (string.len(quizObject)-1))
                quizObject = string.gsub(quizObject, "},{", ",")

                quizObject = "return "..quizObject:gsub('("[^"]-"):','[%1]=')
                print(quizObject)
                quizObject = loadstring(quizObject)()
                print(quizObject.quiz_id)


[TOPIC: post.html]
#4

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 903 posts
  • Corona SDK

Another problem I have encountered a few times with JSON is when there is a gap in the indices, so the keys get explicitly named. 

 

Then when you use json.decode, it treats the keys as strings rather than numbers (e.g. quizObject[3] would fail but quizObject["3"] would work.

 

When decoding json, I always have it attempt to convert all "numeric" string keys into number keys:

function json.toNumericKeys(t)
    local t2 = {}
        for k,v in pairs(t) do
            if type(v) == "table" then
                --recursive call if we have a table of tables
	        json.toNumericKeys(v)
            end
            if tonumber(k) and type(k) ~= "number" then
                t2[tonumber(k)] = v
            else
                t2[k] = v
            end
        end
    return t2
end

local quizObject = json.decode(data)
quizObject = json.toNumericKeys(quizObject)



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: json, corona