Module:Json parameter test: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 4: | Line 4: | ||
-- JSON-Daten in einem Lua-String | -- JSON-Daten in einem Lua-String | ||
local jsonData = frame:callParserFunction('#visualdataquery:[[unique title::+]]', { '?articleid', schema = 'Data classes', format = 'json-raw' }) | local jsonData = frame:callParserFunction('#visualdataquery:[[unique title::+]]', { '?articleid', schema = 'Data classes', format = 'json-raw' }) | ||
-- Debug: Gib die empfangenen JSON-Daten aus | -- Debug: Gib die empfangenen JSON-Daten aus | ||
mw. | mw.logObject(jsonData, "Received JSON Data") | ||
-- Überprüfe, ob jsonData gültig ist | -- Überprüfe, ob jsonData gültig ist | ||
| Line 17: | Line 17: | ||
-- Debug: Gib die getrimmten JSON-Daten aus | -- Debug: Gib die getrimmten JSON-Daten aus | ||
mw. | mw.logObject(jsonData, "Trimmed JSON Data") | ||
-- JSON-Daten in Lua-Tabelle umwandeln | -- JSON-Daten in Lua-Tabelle umwandeln | ||
local success, json = pcall(mw.text.jsonDecode, jsonData) | local success, json = pcall(mw.text.jsonDecode, jsonData) | ||
if not success then | if not success then | ||
mw.logObject(json, "JSON Decode Error") | |||
return "Fehler beim Dekodieren der JSON-Daten: " .. tostring(json) | return "Fehler beim Dekodieren der JSON-Daten: " .. tostring(json) | ||
end | end | ||
mw.logObject(json, "Decoded JSON Data") | |||
local result = {} | local result = {} | ||
Revision as of 07:45, 27 May 2024
Documentation for this module may be created at Module:Json parameter test/doc
local p = {}
function p.generateClassLists(frame)
-- JSON-Daten in einem Lua-String
local jsonData = frame:callParserFunction('#visualdataquery:[[unique title::+]]', { '?articleid', schema = 'Data classes', format = 'json-raw' })
-- Debug: Gib die empfangenen JSON-Daten aus
mw.logObject(jsonData, "Received JSON Data")
-- Überprüfe, ob jsonData gültig ist
if not jsonData or jsonData == '' then
return "Keine JSON-Daten empfangen."
end
-- Entferne führende und nachfolgende Leerzeichen
jsonData = mw.text.trim(jsonData)
-- Debug: Gib die getrimmten JSON-Daten aus
mw.logObject(jsonData, "Trimmed JSON Data")
-- JSON-Daten in Lua-Tabelle umwandeln
local success, json = pcall(mw.text.jsonDecode, jsonData)
if not success then
mw.logObject(json, "JSON Decode Error")
return "Fehler beim Dekodieren der JSON-Daten: " .. tostring(json)
end
mw.logObject(json, "Decoded JSON Data")
local result = {}
for _, item in ipairs(json) do
local title = item.data.title
local classNames = item.data["class names"]
table.insert(result, "== " .. title .. " ==")
table.insert(result, "* " .. table.concat(classNames, "\n* "))
end
return table.concat(result, "\n\n")
end
return p