Static data load

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

static_data_load(path, callback)

static_data_load is used to get load static data from a XML, JSON or CSV file.

Return value

This function won't return any value.

Arguments

# Argument Type Description
1 path String The location of the static file inside the resource folder (*.xml or *.json).
2 callback Function Function is called when data has been loaded. Data argument contains the data, is nil on error.

Example (JSON)

Example person.json file:

{
  "firstName": "John",
  "lastName": "Smith",
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    "212 555-1234",
    "646 555-4567",
    "123 456-7890"
  ]
}

Lua code to read JSON example above

static_data_load("person.json", function(data)
  -- Print first name
  print(data["firstName"])

  -- Print street address
  print(data["address"]["streetAddress"])
  
  -- Print all phone numbers
  for key,value in pairs(data["phoneNumbers"]) do
    print(value)
  end
end)

Example (CSV)

Example cars.csv file:

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!

Lua code to read CSV example above

static_data_load("cars.csv", function(data)
  -- Print all cars
  for key,value in pairs(data) do
    print(value["Year"])
    print(value["Make"])
  end
end)