Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to get info from JSON response? — Gideros Forum

How to get info from JSON response?

ljp1203ljp1203 Member
edited January 2013 in General questions
Hi everyone,

I need some help. I want to know how to get data from a JSON response. I want to make the objectId of the user, their username. But I don't know how to do this. I am also using Parse.

Thanks.
-Landon
JLS
Lead Coder and Designer

Comments

  • This thread: http://www.giderosmobile.com/forum/discussion/144/proof-of-concept-conversion-of-3rd-party-box2d-editor-json-output-to-lua#Item_8

    Mentions reading JSON using: http://json.luaforge.net/

    Also, just in case it wasn't dummy data, your screen shows password information.
  • @zvardin

    Thanks for the help. That was dummy data, so it will be changed. With this post that you gave me, will I be able to make the objectId that you see at the bottom the end part of the url like this:
    			local data = {
    				username = "hex",
    				password = "ahex3z",
    				firstName = "Al",
    				lastName = "Hec",
    				email = "alh1@hotmail.com"
    			}
     
    			local body = Json.Encode(data)
                            local user = objectId -- I want user to be the objectId from the response of the Json.  How would I get the objectId you see in the picture in my last post?
     
     
    			-- Call load the url
    			local url = baseUrl .. "/1/users" .. user --This is what I want it to look like.
    			local urlLoader = UrlLoader.new(url, UrlLoader.POST, headers, body)
    Thanks hopefully you can help me with this!
    -Landon
    JLS
    Lead Coder and Designer
  • Maybe I'm not sure what you're asking, but if you have an ID in the JSON object, I don't see why you couldn't retrieve it and add it on to the end of your URL.
  • @zvardin

    How would I retrieve it?

    Thanks.
    -Landon
    JLS
    Lead Coder and Designer
  • I believe when you handle the COMPLETE event for the URLLoader you can get event.data and then use that JSON library like so:
    local data = json.decode(event.data)
    --data.objectID (or whatever you've called it should then have the information)
  • @zvardin

    That didn't work, it made the player crash :(

    Would you like me to attach the project file to allow you to see whats going on?

    Thanks.
    -Landon
    JLS
    Lead Coder and Designer
  • ar2rsawseenar2rsawseen Maintainer
    edited January 2013
    @ljp1203 crashed with what error?

    1) You need to make sure you have json library added to decode the data
    By looking at your example, you'd probably need to use
    local data = Json.Decode(event.data)
    --and not
    --json.decode(event.data)
    2) put print(event.data) statement before decoding to make sure you receive correct data
    3) Do you really can send body data as json encoded string? Just curious haven't tried that
  • ljp1203ljp1203 Member
    edited January 2013
    @ar2rsawseen

    It doesn't even give me an error. It just closes the player and puts me back on the homescreen. Also, would you like me to add the project file so you can take a look at what's going on?

    I'm also using Parse, if that helps :)

    Thanks.
    -Landon
    JLS
    Lead Coder and Designer
  • Here's a simple test attached that works. There may be complications if you're trying to use Parse etc. You need to make sure you're getting json data back and how it's being named.
    zip
    zip
    JSONTest.zip
    6K
  • @zvardin

    Thanks for the test project. I won't be able test it until I get home from school today at 4 PM EST.

    Also, how would I add Parse's API to the library stated above?

    Sorry about asking these questions, I just don't really understand fully the server side of apps.

    Thanks again!
    -Landon
    JLS
    Lead Coder and Designer
  • I haven't added one myself, I had experimented with one @ndoss was nice enough to have added himself (Kinvey). But basically you're able to define the API in a text file, you could probably look at Kinvey and others that are already defined and use Parse's API documentation to define the functions you need. It shouldn't be too bad since you will probably only need a handful of actual API calls I would assume.
Sign In or Register to comment.