Win an Ipad
  Developer   Documents   JSON Object

JSON Object

This article explains how JSON object is defined in Moscrif; working with JSON objects. Article also shows JSON parser as an example.

JSON Object

JavaSript Object notation represents simple database structures and associative arrays.It is designed as text based human readable. Moscrif defines JSON in script as variable or it can be kept as file (*.mso).

Example of how to define simple JSON object in Moscrif :

var obj = { field: "name", value:"Moscrif"} console <

Example of simple JSON parser class. JSON parser class can read a *.mso Moscrif file, load file and parse JSON object properties:

json.mso file contains

/*
json.mso file contains
{
    propA  : "value1",
      "propB" : "value2",
    propC  : "value3"
}
*/

Parser class

class JSON
{
    function this()
    {
       this._data = null;
    }
    property undefined(k,v)
    {
      get
      {
           var ret = undefined;
           if(this.data[k] !== undefined)
               ret = this.data[k];
           else if(this.data[k.toString()] !== undefined)
               ret = this.data[k.toString()];
           return ret;
       }
    }
    //set data
    property data(v)
    {
       get return this._data;
       set this._data = v;
    }
    //load data from source file
    function load(source)
    {
       var mso = "app://"+source;
       //check if file exists
       if (!System.isFile(mso)) {
           var msg = String.printf("Cannot find %s!", mso);
           System.messageBox(msg);
           throw msg;
       }
       var f = Stream.openFile(mso, "r8");
       // check if file can be read
       if (!f)
           throw String.printf("Cannot load %s!", mso);
       //parse data from file
       this.data = parseData(f);
       f.close();
    }
} var json = new JSON();
//define data
json.data = {
    propA  : "value1",
  "propB" : "value2"
}
console < console < console < json.load("json.mso");
console < console < console <

 

 Object   JSON Object   String stream 
Write a Comment (0)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.