模組:JSONObject

维基百科,自由的百科全书
文档图示 模块文档[创建]
local p = {}

function p._get( frame, args )
	
	local default = args["default"]
	local title = args[1]
	
	local page = mw.title.new( title )
	local content = page:getContent()
	if content == nil then
		return default
	end

	local object = mw.text.jsonDecode( content )
	
	local idx = 2
	while args[idx] ~= nil do
		if object[args[idx]] ~= nil then
			object = object[args[idx]]
		elseif object[tonumber(args[idx])] ~= nil then
			object = object[tonumber(args[idx])]
		elseif default ~= nil then
			return default
		else
			return nil
		end
		idx = idx + 1
	end
	
	if type(object) == "table" then
		return default
	end
	
	return object
end

function p.get( frame )
	local args = frame:getParent().args
	
	result = p._get( frame, args )
	if args["preprocess"] then
		result = frame:preprocess( result )
	end
	
	return result
end

return p