[plugin] JSON (import/export/generate/edit/inspect/...)

  • Pulling my hair out trying to set the text from an item in the json produced here:

    http://api.tvmaze.com/search/shows?q=walking+dead

    How can I set a text object to the value of the chosen key?

    Example: value "4.401577" from key "score".

  • Looking at your JSON, you have an array of objects with two keys, "score" and "show"

    So to get the score of the first object, you can do it like this:

    JSON.Value(0,0,"score")[/code:9b1y66wd]The first 0 means "from root", the seconds means, "first element", and then "key score"
    
    in javascript it would correspond to [code:9b1y66wd]root[0]["score"][/code:9b1y66wd]
  • Looking at your JSON, you have an array of objects with two keys, "score" and "show"

    So to get the score of the first object, you can do it like this:

    JSON.Value(0,0,"score")[/code:r5sc64jx]The first 0 means "from root", the seconds means, "first element", and then "key score"
    
    in javascript it would correspond to [code:r5sc64jx]root[0]["score"][/code:r5sc64jx]
    

    You're a life-saver. Thanks.

  • Yann

    This is quite a powerful plugin, thanks for creating it. I was wondering if you plan on updating the plugin to address this issue:

    thanks nice finding, I was doing

    delete array[0];[/code:3s15j0yz]
    but this doesn't change the size of the array so javascript set it to undefined (which is transformed to null on export)
    I have to do [code:3s15j0yz]array.splice(0,1);[/code:3s15j0yz]
    

    I have a workaround where I clear the entire array, and recreate it sans the deleted values, but it would be a lot cleaner to delete the value directly.

  • zatyka

    yeah I'll probably release the fix someday, but I had started implementing object references to give a bit more power to the plugin, but got stuck with how to import/export references using JSON.

    I found some possible solution from dojo where they have a specific syntax for references but I didn't have time to really wrap my head around that to extract what I need from dojo (I really don't want to add dojo to the plugin just for their JSON functions)

    So, yeah... someday :D

    I could just release a fix on the array bug, but.... sadly... I'm not using version control so... <_<

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yann

    Gotcha. No worries, thanks for the update.

  • Hi, great plugin!!

    just some question (sorry):

    is it possible to count the objects?

    e.g. I have one json file with several objects (like wizard, hunter,...) and I want that the program counts them and put it in a global variable.

    In this case: there are wizard, hunter, Knight, farmer -> global variable: 4

    if not: I named the objects 1,2,3 and 4. Can it put the highest object in the variable. I tried it with instance.variable, but it always says "0"

    ------------------------------------------

    [quote:12vq0z5k]other question: is it possible to choose/open a json file with function?

    e.g. function(car)

    in the function: set text to car.value(0,0,1)

    I tried with variable, but the program didn't accepted it

    e.g. function(car)

    set function.param(0) to Database

    set text to database.value(0,0,1) <- error

    I would like to use one function instead making it for several databases.

    [solved] I had to use: " pick by comparison "

  • Yann

    Very usefull plugin, but I think there's a bug in "Get Size" method:

    for each property at current > JSON.Size(JSON.CurrentKey) always returns 2, no matter how I try

    OK, found it out. It should be similar to the array's "Length" method, with an argument (1):

    for each property at current >JSON.Size(1)

  • edwardr

    you can count objects by the method I described above^

  • Yann Very nice work my friend! Working well so far. One trick escapes me: I have an array of objects I need to get into.

    How would I get to the attribs in an object within an array? Like...

    {
        "_id": "blahblahid",
        "units": [
            { "unitId": "blahUnitID1", "qty": 7 },
            { "unitId": "blahUnitID2", "qty": 5 },
            { "unitId": "blahUnitID3", "qty": 3 },
        ]
    }
    [/code:5q4ibz47]
    
    How would I get to the units[1].unitId or units[2].qty?
  • locohost

    from memory I'd say:

    JSON.Value(0, "units", 1, "unitID")
    JSON.Value(0, "units", 2, "qty")[/code:1w7j9ogo]
    
    the arguments are to be read as:
    1st: 0 = root, 1 = current  (current is used un foreach loops and I think you also have an action to set it)
    2nd: "units" is one of the key of your root object's member (if the root were an array , you would use the index)
    3rd: the index of the "units" array
    4th: "unitId" or "qty" one of the key of one of the object in the "units" array
  • Seems to be working. Thank you very much!!

  • Yann

    I seem to be having a issue with C2 working with JSON data. I am simply trying to load a JSON raw into your plugin

    {
    "Bob":{
         "Skill1":"AA",
         "Skill2":"BA",
         "Skill3":"CA"},
    "Ted":{
         "Skill1":"AB",
         "Skill2":"BB",
         "Skill3":"CB"},
    "Mike":{
         "Skill1":"AC",
         "Skill2":"BC",
         "Skill3":"CC"}
     }
    [/code:179ffpjs]
    
    What is wrong with this syntax? The first "{" has a red line under it so there has to be something wrong with how I am wighting this JSON. Your plugin seems to be just exactly what I need but I have no clue as to why my code is getting this error.
    
    I'm sure its something that I am doing stupid
    
    I can get it to work by changing it to
    
    [code:179ffpjs]
    "{
    ""Bob"":{
         ""Skill1"":""AA"",
         ""Skill2"":""BA"",
         ""Skill3"":""CA""},
    ""Ted"":{
         ""Skill1"":""AB"",
         ""Skill2"":""BB"",
         ""Skill3"":""CB""},
    ""Mike"":{
         ""Skill1"":""AC"",
         ""Skill2":""BC"",
         ""Skill3"":""CC""}
     }"
    [/code:179ffpjs]
    
    But then its not a JSON and having too many " means just that much more likely that I will miss one and screw the whole thing.
  • CrazyVulcan

    That's because C2 expects a string, a string in C2 is some characters (or none) surrounded by double quotes (")

    Problem is, what happens if you want you string to contains double quotes?

    Answer: you need to escape them.

    How to escape them?

    some languages use \" C2 use double double quote ""

    As a small remark, C2 isn't THAT weird in this area, you also do that if you want to escape double quotes in verbatim string literals in C#

    http://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal

    Now, one way to avoid this messy escaping is to put your pretty json inside a file you import in the project, and you get retreive it using Ajax.

  • CrazyVulcan

    That's because C2 expects a string, a string in C2 is some characters (or none) surrounded by double quotes (")

    Problem is, what happens if you want you string to contains double quotes?

    Answer: you need to escape them.

    How to escape them?

    some languages use \" C2 use double double quote ""

    As a small remark, C2 isn't THAT weird in this area, you also do that if you want to escape double quotes in verbatim string literals in C#

    http://stackoverflow.com/questions/1928909/can-i-escape-a-double-quote-in-a-verbatim-string-literal

    Now, one way to avoid this messy escaping is to put your pretty json inside a file you import in the project, and you get retreive it using Ajax.

    Thanks Yann, sure enough using AJAX did the trick. Quick question how big a JSON file can your plugin handle out of curiosity? Im sure I will find out but in my game each player has the ability to unlock various things as well as saving user created items that they can use later. I would love to just put all of it in one JSON using various tables if posable. Dose that seem doable?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)