How to parse JSON Data in Corona SDK [duplicate] - coronasdk

I'm trying to make an app where I will gather information from a json api http://pool-x.eu/api, and print information easly bo choosing parameter.
What is the easiest way to print each of the informations?
Was thinking something in the way of making the information a string, and then request each of the parameters that way, but I don't know if that's the way to do it.

here's a sample code to decode the json data i just happen to make a json text file out if the link you gave and decode it hope it helps
local json = require "json"
local txt
local path = system.pathForFile( "json.txt", system.ResourceDirectory )
local file = io.open( path, "r" )
for line in file:lines() do
txt = line
end
print(txt)
local t = json.decode( txt )
print(t["pool_name"])
print(t["hashrate"])
print(t["workers"])
print(t["share_this_round"])
print(t["last_block"])
print(t["network_hashrate"])

Related

Unable to use io.read() to grab the user's input after already using it to grab a file's contents

I've got this program that starts off with the program grabbing a file's contents.
local oldprint = print
local print = io.write
-- this was done mainly because the newline from print() wasn't needed
io.input("script.txt")
local script = io.read("*all")
io.close()
Then there is a function containing another io.read(), which should allow for the user's input.
local functions = {
[","] = function()
local input
repeat
print("\nAwaiting input... (must be a number)\n")
input = io.read("*n")
until input ~= nil
array[pointer] = input
print(stringy)
end
}
functions[","]()
I expect it for it to print once, and then grab the input, but it ends up constantly printing.
I've tried using io.flush(), but it didn't work, and I'm not exactly sure what else to try...
By calling io.input("script.txt") you set that file as the default input file. Any following calls to io.read() will hence read from that file.
Either use file:read instead of io.read or reset the input to the standard input stream by calling io.input(io.stdin).
I suggest you refer to the Lua reference manual.
https://www.lua.org/manual/5.4/manual.html#6.8
To use io.read(), you have to close the file you opened, because if you have a file open, lua will assume you are reading from the file that you opened.
Try grabbing the input before you open the file

Lua is refusing to read from a file

I typed up my code not expecting it to work first try, and of course it didn't. I kept tweaking it for hours on end, but I kept getting the same result until I made as simple as possible.
local file = io.open("File_Name", "r")
io.output(file)
local test = io.read('*all')
io.close(file)
print(test)
After getting (no return) from this, I've decided to take a break and let someone else answer my question.
The problem with your code is that you're trying to read from whatever is defined as your input file. You only opened a file, but you didn't tell Lua to use it as the input file, so io.read won't read from the opened file, yet.
local file = io.open(filename, "r")
local test = file:read("a")
io.close(file)
print(test)
Alternatively:
local file = io.open(filename, "r")
io.input(file)
local test = io.read("a")
io.close(file)
print(test)
or
local file = io.open(filename, "r")
local test = io.input(file):read("a")
io.close(file)
print(test)
Of course you should check wether opening the file succeeded befor using the file handle.
Depending on your Lua version the read format is either *a or a. I cannot remember if both is ok in all versions. At least that's what the manual says.

How to insert file lines into table in lua

I'm trying to make a Discord bot with lua and its going well so far, but I'm having a couple problems with the IO portion of lua.
I'm trying to read a large list.txt file in lua and inserting each line into a table, but so far all of my attempts didn't work.
Any advice?
Attempt #1 spits out nil:
local open = io.open
local function read_file(path)
local file = open(path, "r") -- r read mode and b binary mode
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
return content
end
local fileContent = read_file("list.txt")
local vga_files = {}
table.insert(vga_files, fileContent)
I was not able to replicate your error running your code. Your code is valid, and is likely doing what you asked it to do.
here you tell the read_file function to return nil:
if not file then return nil end
So if the file is not found you will get nil. A good step in debugging would be to add a print in the body of this if statement and see if it is getting entered.
When you call read_file:
local fileContent = read_file("list.txt")
you pass in only a file name, this means lua will look for that where ever the code is being executed, and this location maybe different from what you expect.
I validated your code works by pointing the read at itself, and printing the result.
local open = io.open
local function read_file(path)
local file = open(path, "r") -- r read mode and b binary mode
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
return content
end
print(read_file("so_io_read_test.lua"))
Additionally to get the lines you should really used io.lines which creates an iterator that you can use in a for loop.
local vga_files = {}
for line in io.lines("list.txt") do
table.insert(vga_files, line)
end
Or alternatively you can read the file and then split the lines using gmatch.
local contents = read_file("so_io_read_test.lua")
for line in contents:gmatch("([^\n]+)") do
table.insert(vga_files, line)
end

How to open base64 spreadsheet on Ruby

I've been trying to manipulate a file that's base64 encoded that I'm recieving from my client.
I'm currently using https://github.com/zdavatz/spreadsheet/blob/master/GUIDE.md to manipulate it, however, there doesn't appear to be any way to open a file directly from the base64 blob, or should I write it and then read from it? can't that a potential security threat for the server?
for example, if I recieve a file :
file = params[:file] with contents:
data:application/vnd.ms-excel;base64,0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAOwADAP7
(should I remove the data:application/vnd.ms-excel;base64, ?)
I'd like to open it with this:
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open "#{Rails.root}/app/assets/spreadsheet/event.xls"
(or with a blob or temp fle)
Sorry if it's pretty obvious, been looking for hours and there's not much info about it available, tried creating a temp file first but I don't think that's supported and there's not much I can get from the docs.
Shot in the dark: Maybe decode it, write to binary-enabled tempfile, and then feed that to Spreadsheet?
tmpfile = Tempfile.new.binmode
tmpfile << Base64.decode64(params[:file])
tmpfile.rewind
book = Spreadsheet.open(tmpfile)

Download file by url in lua

Lua beginner here. :)
I am trying to load a file by url and somehow I am just too stupid to get all the code samples here on SO to work for me.
How to download a file in Lua, but write to a local file as it works
downloading and storing files from given url to given path in lua
socket = require("socket")
http = require("socket.http")
ltn12 = require("ltn12")
local file = ltn12.sink.file(io.open('test.jpg', 'w'))
http.request {
url = 'http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg',
sink = file,
}
my program runs for 20 - 30s and afterwards nothing is saved. There is a created test.jpg but it is empty.
I also tried to add w+b to the io.open() second parameter but did not work.
The following works:
-- retrieve the content of a URL
local http = require("socket.http")
local body, code = http.request("http://pbs.twimg.com/media/CCROQ8vUEAEgFke.jpg")
if not body then error(code) end
-- save the content to a file
local f = assert(io.open('test.jpg', 'wb')) -- open in "binary" mode
f:write(body)
f:close()
The script you have works for me as well; the file may be empty if the URL can't be accessed (the script I posted will return an error in this case).

Resources