JSON data into Coll

peter nelson's icon

hi, I have not used javascript before, but have data that I want to use in JSON files.
I can parse the files this way:
function read(p) {
    memstr = "";
    data = "";
    maxchars = 800;
    path = p;
    var f = new File(path, "read");
    f.open();
    if (f.isopen) {
        while(f.position<f.eof) {
            memstr+=f.readstring(maxchars);
        }
        f.close();
    } else {
        post("error\n");
    }
    UI = JSON.parse(memstr);
    }

    function dump(){
        for(i in UI){
            for(j in UI[i]){
                return("\n",i,j,UI[i][j]);
                }
            }
        }
However, I cannot sort out how to get this data into a coll object. Any tips?

Peter

tyler mazaika's icon

check out the Dict JavaScript object docs. You can use setparse to read a JSON string and then push_to_coll. Or possibly just read the JSON file directly into the Dict.

peter nelson's icon

OK I'll have a look - thanks!

peter nelson's icon

Sorry - I really am a novice - where will I find those? I looked here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript
but can't find Dict.
P

tyler mazaika's icon

https://docs.cycling74.com/max8/vignettes/javascriptinmax

peter nelson's icon

Great! thank you.

peter nelson's icon

hey Tyler, done it! thanks so much - your hints made it possible.

P