Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

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

- - - - -
7 replies to this topic
json corona
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/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

  • 770 posts
  • Corona SDK

Tables start at position 1 in lua, not 0.

 

Try changing 0 to 1.

 

print(data[1].quiz_id)



[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

  • 923 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: post.html]
#5

ininraicli.m

[GLOBAL: userInfoPane.html]
ininraicli.m
  • Observer

  • 1 posts
  • Corona SDK

I get the credentials.json file when I download the client configuration files.



[TOPIC: post.html]
#6

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

Hello and welcome to the Corona forums!

 

Can you provide more context about what you're doing? There isn't enough here to even begin to guess as to where to help you.

 

Read this post to help learn to ask better questions: 

 

https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

 

Rob



[TOPIC: post.html]
#7

rinbsex

[GLOBAL: userInfoPane.html]
rinbsex
  • Observer

  • 1 posts
  • Corona SDK

The error handler of an $.ajax request has the signature

 

Function( jqXHR jqXHR, String textStatus, String errorThrown )

[…] receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. TTRockStars

 

Change your function to

error: function(jqXhr) {
    var data = jqXhr.responseJSON; // you saw this in your console.log
    if (data) {
        …
    } else {
        // there might be other errors, where you don't get the server message
    }
}


[TOPIC: post.html]
#8

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

Hello and welcome to the Corona Labs forums. This is not the best place to ask JavaScript/JQuery questions. These forums are for people to discuss using Corona and Lua to build mobile apps and games.

 

You will find better answers else where. Perhaps try StackOverflow!

 

Rob




[topic_controls]
[/topic_controls]