How to make an app - Andromo App Maker

Announcement

Collapse
No announcement yet.

JSON.Decode confusion???

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSON.Decode confusion???

    Can someone help me WTF is going on here?
    I'm trying to use Sakuya's JSON plugin to pull movie data from a JSON-encoded string (which is being retrieved via private API-Key from https://www.themoviedb.org)

    I thought JSON.Decode was supposed to decode the entire JSON-encoded string into table? Right?
    But when I run the following code:
    Code:
    -- Nb. sReturn variable contains JSON-encoded string being returned from https://www.themoviedb.org via private API key.
    
    sReturn = [[{"page":1,"total_results":2,"total_pages":1,"results":[{"vote_count":3557,"id":75780,"video":false,"vote_average":6.4,"title":"Jack Reacher","popularity":19.129319,"poster_path":"\/38bmEXmuJuInLs9dwfgOGCHmZ7l.jpg","original_language":"en","original_title":"Jack Reacher","genre_ids":[80,18,53],"backdrop_path":"\/ezXodpP429qK0Av89pVNlaXWJkQ.jpg","adult":false,"overview":"When a gunman takes five lives with six shots, all evidence points to the suspect in custody. On interrogation, the suspect offers up a single note: \"Get Jack Reacher!\" So begins an extraordinary chase for the truth, pitting Jack Reacher against an unexpected enemy, with a skill for violence and a secret to keep.","release_date":"2012-12-20"},{"vote_count":2317,"id":343611,"video":false,"vote_average":5.4,"title":"Jack Reacher: Never Go Back","popularity":17.9223,"poster_path":"\/IfB9hy4JH1eH6HEfIgIGORXi5h.jpg","original_language":"en","original_title":"Jack Reacher: Never Go Back","genre_ids":[28],"backdrop_path":"\/nDS8rddEK74HfAwCC5CoT6Cwzlt.jpg","adult":false,"overview":"Jack Reacher must uncover the truth behind a major government conspiracy in order to clear his name. On the run as a fugitive from the law, Reacher uncovers a potential secret from his past that could change his life forever.","release_date":"2016-10-19"}]}]]
    tResult = JSON.Decode(sReturn);
    
    Debug.Clear();
    Debug.ShowWindow(true);
    
    for k,v in pairs (tResult) do
        Debug.Print(k..": "..tostring(v).."\r\n");
    end
    ... all I get are the first 3 elements from the string.
    This is what it returns:
    Code:
    total_results: 2
    total_pages: 1
    page: 1
    Huh? How the heck do I actually get the essential info from the encoded-string? Like movie-title, plot, poster-url, etc? I thought that was the whole point? Or am I misunderstanding the purpose of this plugin altogether?

    Nb. (Apz and JSON Plugin attached for reference)
    Attached Files

  • #2
    Edit,
    Hmm, okay. Just noticed something 'wiggy' going on with that so-called JSON-encoded string. There's a bunch of non-alphanumeric character clashes in there which AMS doesn't like. Yet, that is the exact JSON string coming back from themoviedb.org via my API key.

    Any ideas on how to get around this? I'd rather not start string.gsub-ing all over the place. It would kind of defeat the whole point of the plugin.

    Comment


    • #3
      Done some more digging. Found a json.lua script which basically does the same thing as Sakuya's plugin.

      But am having similar problems. The json.encode command works as expected. But the json.decode command returns the first 3 values only. Then it throws an error when decoding the last part of the JSON string.

      Could one of you take a look at my APZ here (attached below)? And show what I'm doing wrong. How to return ALL the json.decode values correctly?
      Attached Files

      Comment


      • #4


        As you can see from this message that last one did indeed get the x table file, so I not sure why it wouldn't be on your computer, have to change anything in ams, I got some error like this when I used another lua dll in the past.


        Decode worked with this
        PHP Code:
        tReturn json.decode('[6,7,9,{"x":10}]') -- Returns 679, { 10 } }
        for 
        k,v in pairs(tReturn) do
            if 
        type(v) == "table" then
                
        for i,e in pairs(v) do
                    
        Dialog.Message("Table Key-Value pairs from JSON decoded string"i..": "..eMB_OKMB_ICONNONEMB_DEFBUTTON1);
                
        end
            
        else
                
        Dialog.Message("Table Key-Value pairs from JSON decoded string"k..": "..vMB_OKMB_ICONNONEMB_DEFBUTTON1);
            
        end
        end 
        Without giving me the table error
        BunnyHop CDD Obscurer Download No Longer Supported
        Plugins or Sources MokoX

        Comment


        • #5
          You need to walk the tables recursively in order to get all the values. Modified example attached.

          Ulrich
          Attached Files

          Comment


          • #6
            Originally posted by Ulrich View Post
            You need to walk the tables recursively in order to get all the values. Modified example attached.

            Ulrich
            So this a more complex way of what I did, but makes it a nicer function, well that's what it looks like.
            BunnyHop CDD Obscurer Download No Longer Supported
            Plugins or Sources MokoX

            Comment


            • #7
              @Rexxy,

              You da man! Thanks, Rexxy. I didn't actually realize it was necessary to iterate over table values within a JSON string. I thought the whole point of working with a JSON-encoded string and a JSON-decode function was to avoid having to do all that. Figured that kind of 'heavy lifting' was done by the json.lua script itself. And indeed is what I thought Sakuya's plugin would do.

              However, upon closer reading of the Notes & ReadMe.txt that came with the json.lua script, it actually warns about "arrays, tables with mixed key types" throwing errors. LOL, that'll teach me to read the instructions properly.

              Anyway, I see that your code is working with Sakuya's plugin, too.
              Great stuff!

              ....................................
              @Ulrich,

              Hey Ulrich, it's nice to hear from you again. Don't seem to see as much of you in the AMS subforums as we did in previous years? Guess you're pretty busy dealing with SUF & MSIF these days, though.

              Many thanks for modified APZ. Quite a bit in there to digest - but digest I shall. For a moment there, I thought I'd have to start s:gmatching & s:gsubbing all through those monstrous JSON strings from the themoviedb.org API. Which would've probably been a recipe for the mother of all Lua-induced migraines. LOL, I think you just might have saved me mega$ on headache medications.

              This does look pretty awesome, though. And will make things MUCH easier. Once I comprehend the recursion, that is.. Ah, cuppla dayzz!

              Cheers, my man.. And all the best with Mindquake, yeah?

              Nb. Interestingly enough, this also works with Sakuya's plugin.

              Comment


              • #8
                Originally posted by BioHazard View Post
                @Rexxy,

                You da man! Thanks, Rexxy. I didn't actually realize it was necessary to iterate over table values within a JSON string. I thought the whole point of working with a JSON-encoded string and a JSON-decode function was to avoid having to do all that. Figured that kind of 'heavy lifting' was done by the json.lua script itself. And indeed is what I thought Sakuya's plugin would do.

                However, upon closer reading of the Notes & ReadMe.txt that came with the json.lua script, it actually warns about "arrays, tables with mixed key types" throwing errors. LOL, that'll teach me to read the instructions properly.

                Anyway, I see that your code is working with Sakuya's plugin, too.
                Great stuff!

                ....................................
                @Ulrich,

                Hey Ulrich, it's nice to hear from you again. Don't seem to see as much of you in the AMS subforums as we did in previous years? Guess you're pretty busy dealing with SUF & MSIF these days, though.

                Many thanks for modified APZ. Quite a bit in there to digest - but digest I shall. For a moment there, I thought I'd have to start s:gmatching & s:gsubbing all through those monstrous JSON strings from the themoviedb.org API. Which would've probably been a recipe for the mother of all Lua-induced migraines. LOL, I think you just might have saved me mega$ on headache medications.

                This does look pretty awesome, though. And will make things MUCH easier. Once I comprehend the recursion, that is.. Ah, cuppla dayzz!

                Cheers, my man.. And all the best with Mindquake, yeah?

                Nb. Interestingly enough, this also works with Sakuya's plugin.
                Ya far as I know the json lua and plugin both return almost the same way but has a few little limistaions between the two, I use the plugin over the lua these days, but there is a few rare times I use one of the lua versions.

                Ya it's just a basic json array to lua table and table to json array, all the hard work after this we have to do, maybe a plugin using the json lua with extra options would be a thing you could work on lol people still want plugins, I still working on plugin ideas as I what 1.1 plugin creator but I do have a few problems with it some times so I get dishearted lol

                But glad you have been able to work with it, I used the plugin tho over the json lua for my konachan app what pulls there json return to get there images as it was little faster and handled it better, not sure why.
                BunnyHop CDD Obscurer Download No Longer Supported
                Plugins or Sources MokoX

                Comment


                • #9
                  This page from lua-users.org is quite interesting:

                  A comparison of JSON modules
                  http://lua-users.org/wiki/JsonModules

                  Some of the links to the various modules listed there are dead, unfortunately. But the 4th one in the list, especially caught my attention. Looks quite promising. And is a pure Lua solution. (Released under a Creative Commons License):Cool bananas!

                  Comment


                  • #10
                    Originally posted by BioHazard View Post
                    This page from lua-users.org is quite interesting:

                    A comparison of JSON modules
                    http://lua-users.org/wiki/JsonModules

                    Some of the links to the various modules listed there are dead, unfortunately. But the 4th one in the list, especially caught my attention. Looks quite promising. And is a pure Lua solution. (Released under a Creative Commons License):Cool bananas!
                    That is the one I have used on some projects in the past, its worked well for me in the past, but the plugin these days works fine for me two so I lazy and just us the plugin lol
                    BunnyHop CDD Obscurer Download No Longer Supported
                    Plugins or Sources MokoX

                    Comment


                    • #11
                      Originally posted by kingzooly View Post

                      That is the one I have used on some projects in the past,..
                      I like the fact that this one also does a "pretty-printed" version of JSON:encode. Very pretty, LOL.

                      Comment


                      • #12
                        Originally posted by BioHazard View Post
                        I like the fact that this one also does a "pretty-printed" version of JSON:encode. Very pretty, LOL.
                        Well I guess that is a win lol
                        BunnyHop CDD Obscurer Download No Longer Supported
                        Plugins or Sources MokoX

                        Comment


                        • #13
                          PureBasic Woes:

                          Hey, check this out, I've ventured one small step further with JSON. And have attempted to create my very own JSON.Decode DLL in PureBasic.

                          I'm almost there, I think. Except for one little thing which I can't quite work out. Damm it! And I was so close to victory on this one. D'oh! Does anyone know what's wrong with my PB code here?(For some strange reason, it seems this forum won't permit the posting of PB Code, so I've zipped and attached the .pb file).

                          But here's the sticking point:
                          If I code a MessageRequester (for testing purposes) into my DLL. And then call it from AMS, it returns the decoded JSON values, no problemo. But if I try to return the values with a ProcedureReturn command instead, it starts returning blankety-blanks!

                          So why doesn't PB like my ProcedureReturn command? The temperamental little blighter's been taunting me all night. It's something really simple, right? I'm having yet another Homer Simpson moment, right?
                          Attached Files

                          Comment


                          • #14
                            Im trying to figure out how to benefit from this JSONDecode for other use.

                            Is it possible to build an app in AMS that can deal with Trello API ?? to send and receive ?? or this is something else ??

                            And if its not possible in AMS , how can we build this app that can send and receive actions or commands to Trello API.

                            Comment


                            • #15
                              Originally posted by sameer valva View Post
                              Im trying to figure out how to benefit from this JSONDecode for other use.

                              Is it possible to build an app in AMS that can deal with Trello API ?? to send and receive ?? or this is something else ??

                              And if its not possible in AMS , how can we build this app that can send and receive actions or commands to Trello API.
                              If it wasn't possible you be asking in the wrong place, but long as Trallo just returns normal json then I don't see why not, I have used 100's of online apis before in to ams, depending if you need headers setting to send infomation with trello is another things and not down to anyone here on the ams forums.

                              Simple reply is yes it should work.

                              Longer reply is, it might not work, this is down the the services and how they request data.
                              BunnyHop CDD Obscurer Download No Longer Supported
                              Plugins or Sources MokoX

                              Comment

                              Working...
                              X