Difference between revisions of "Static data load"
Jump to navigation
Jump to search
Line 56: | Line 56: | ||
end | end | ||
end) | end) | ||
</source > | </source> | ||
== Example (CSV) == | |||
Example countries.csv file: | |||
<source lang="csv"> | |||
{ | |||
"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" | |||
] | |||
} | |||
</source> | |||
Lua code to read JSON example above | |||
<source lang="lua"> | |||
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) | |||
</source> |
Revision as of 16:17, 28 August 2018
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 countries.csv 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)