Module:JSONUtils: Difference between revisions
Jump to navigation
Jump to search
(Created page with "local JSONUtils = {} local json = require('json') function JSONUtils.parse(jsonStr) -- JSON-String in Lua-Tabelle konvertieren local data = json.parse(jsonStr) -- Durch die Daten iterieren und sie ausgeben for _, item in ipairs(data) do -- Pagetitle ausgeben print(item.pagetitle) -- Daten ausgeben for _, class in ipairs(item.data.classes) do print('Class Name: ' .. class['class name']) for _, subClass in ipairs(class['su...") |
No edit summary |
||
Line 1: | Line 1: | ||
local JSONUtils = {} | local JSONUtils = {} | ||
function JSONUtils.parse(jsonStr) | function JSONUtils.parse(jsonStr) | ||
-- JSON-String in Lua-Tabelle konvertieren | -- JSON-String in Lua-Tabelle konvertieren | ||
local decodedData = mw.text.jsonDecode(jsonStr) | |||
-- Durch die Daten iterieren und sie ausgeben | |||
for _, item in ipairs(decodedData) do | |||
-- Pagetitle ausgeben | |||
print(item.pagetitle) | |||
-- Daten ausgeben | |||
for _, class in ipairs(item.data.classes) do | |||
print('Class Name: ' .. class['class name']) | |||
for _, subClass in ipairs(class['sub-classes']) do | |||
print('\tSubclass Name: ' .. subClass['sub-class name']) | |||
for _, subSubClass in ipairs(subClass['sub-sub-classes']) do | |||
print('\t\tSubsubclass Name: ' .. subSubClass['sub-sub-class name']) | |||
print('\t\tValue: ' .. subSubClass.value) | |||
end | |||
end | |||
end | |||
end | |||
end | end | ||
return JSONUtils | return JSONUtils |
Revision as of 08:21, 2 May 2024
Documentation for this module may be created at Module:JSONUtils/doc
local JSONUtils = {}
function JSONUtils.parse(jsonStr)
-- JSON-String in Lua-Tabelle konvertieren
local decodedData = mw.text.jsonDecode(jsonStr)
-- Durch die Daten iterieren und sie ausgeben
for _, item in ipairs(decodedData) do
-- Pagetitle ausgeben
print(item.pagetitle)
-- Daten ausgeben
for _, class in ipairs(item.data.classes) do
print('Class Name: ' .. class['class name'])
for _, subClass in ipairs(class['sub-classes']) do
print('\tSubclass Name: ' .. subClass['sub-class name'])
for _, subSubClass in ipairs(subClass['sub-sub-classes']) do
print('\t\tSubsubclass Name: ' .. subSubClass['sub-sub-class name'])
print('\t\tValue: ' .. subSubClass.value)
end
end
end
end
end
return JSONUtils