JSON structure help needed

Oh, not changing colors I would suspect to be a bug. There are a few features that don’t seem to work right like setting the background on a navigation bar [import]uid: 19626 topic_id: 13975 reply_id: 62172[/import]

The widgets in the code above is done like the widget sample code, they are delared as globals in main and referenced in other modules as locals. I don’t know if that’s good or not, but since the creators of widgets did it that way i figured it was right.

I changed it though.

I realized what was wrong with the cell colors, I had a " , " at the end of “downColor”. I totally missed that.

I’ve read the API and got this but I get an error

Runtime error  
 ...8ncjymmw0btj00000gn/T/TemporaryItems/212/screen2.lua:72: attempt to index field '?' (a nil value)  
stack traceback:  
 [C]: ?  
 ...8ncjymmw0btj00000gn/T/TemporaryItems/212/screen2.lua:72: in function 'new'  

So I added forward references but it still gives the same error. Then I commented out line 72 and I got no error and nothing got displayed on screen, so now I’ve been staring my self blind on the code and I’m stuck.

I’ve seen that some people put the require(moduleName) inside the function new(), what is the difference?

module(..., package.seeall)  
  
local json = require("json")  
local widget = require("widget")  
  
function new()  
 local g = display.newGroup()  
  
 local newToolbar = widget.newToolbar  
 local newButton = widget.newButton  
 local newTableView = widget.newTableView  
  
 local myNavBar;  
 local myBackBtn;  
 local background;  
  
 local fileEntries = {};  
 local category = {};  
 local categoryName;  
 local myTableView;  
 local itemData = {};  
  
 local onRelease;  
 local onItemRelease;  
  
 -- Forward references for downloading of the json file.  
 local networkListener;  
 local gigsFileName = "gigs.json";  
  
 local path = system.pathForFile(gigsFileName, system.ResourceDirectory)  
 local fileContent = ""  
 local file = io.open(path, "r")  
  
 if file then  
  
 local fileContent = file:read("\*a")  
  
 fileEntries = json.decode(fileContent)  
 --print(entries)  
 category = ""  
 local j = 1  
 for i = 1, #fileEntries do  
 if fileEntries[i].month == category then  
 itemData[j] = {}  
 --itemData[j].icon = itemIcon;  
 itemData[j].title = {  
 label = fileEntries[i].place,  
 font = "Helvetica-Bold",  
 size = 18,  
 color = { 0, 0, 0 },  
 left = 10,  
 top = 10  
 }  
 itemData[j].subtitle = {  
 label = fileEntries[i].time .. " - " .. fileEntries[i].admission,  
 font = "Helvetica",  
 size = 14,  
 color = { 150, 150, 150 },  
 left = 10,  
 top = 0  
 }  
 itemData[j].onRelease = onItemRelease  
 else  
 category = fileEntries[i].month  
 itemData[j].CategoryName = category --This is the line that causes the error.  
 end  
 j = j + 1  
 end  
 end  
 local myTableView = newTableView {  
 x = 0,  
 y = 64,   
 rowHeight = 61,   
 height = 384,   
 width = 320,   
 backgroundColor = "none",  
 bottomLineColor = {218, 230, 241},  
 rowColor = {231, 235, 237},  
 downColor = {218, 230, 241}  
 }  
 g:insert( myTableView.view )  
 myTableView:sync(itemData[j])  
  
 myNavBar = newToolbar ("Gigs")  
  
 g:insert(myNavBar.view)  
  
 function g:cleanUp()  
  
 -- Remove widgets.  
 display.remove(myNavBar);  
 myNavBar = nil;  
 display.remove(myTableView);  
 myTableView = nil;  
  
 -- Remove the entire group.  
 g:removeSelf();  
 g = nil;  
  
 print(" cleanUp");  
 end  
  
 return g  
end  
  

Anyways, Thanks for helping Rob.
/David [import]uid: 34126 topic_id: 13975 reply_id: 62238[/import]

If you’re not comfortable with JSON, but ARE comfortable with Lua tables, you could always create the table structure in Lua, then do a print statement on json.decode( table ).

Then, paste it into this handy tool and click ‘Format’:

http://jsoneditor.appspot.com/ [import]uid: 52430 topic_id: 13975 reply_id: 62242[/import]

I had plans on adding info to the json file and put it in a dropbox or on a webserver so I can update if there are any more gigs. If I make lua table then it has to be in the source compiled right? [import]uid: 34126 topic_id: 13975 reply_id: 62247[/import]

The error is still not being very helpful.

Can you put some prints in there and see what your getting data wise after you read the file and decode the json?

You can use this function

function print\_r ( t )   
 local print\_r\_cache={}  
 local function sub\_print\_r(t,indent)  
 if (print\_r\_cache[tostring(t)]) then  
 print(indent.."\*"..tostring(t))  
 else  
 print\_r\_cache[tostring(t)]=true  
 if (type(t)=="table") then  
 for pos,val in pairs(t) do  
 if (type(val)=="table") then  
 print(indent.."["..pos.."] =\> "..tostring(t).." {")  
 sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8))  
 print(indent..string.rep(" ",string.len(pos)+6).."}")  
 elseif (type(val)=="string") then  
 print(indent.."["..pos..'] =\> "'..val..'"')  
 else  
 print(indent.."["..pos.."] =\> "..tostring(val))  
 end  
 end  
 else  
 print(indent..tostring(t))  
 end  
 end  
 end  
 if (type(t)=="table") then  
 print(tostring(t).." {")  
 sub\_print\_r(t," ")  
 print("}")  
 else  
 sub\_print\_r(t," ")  
 end  
 print()  
end  

to dump a whole table:

print_r(fileEntries) right after you call json.decode and make sure it parsed your data correctly.

While double checking your code, I found the problem… So I was going to delete all the above, but its still good stuff to check out.

Like I said, I didn’t test that code. Right before the line that’s giving you the error, add this line:

 itemData[j] = {}  

We need to initialize itemData[j] to be a table.

As for putting the require inside the function new() if it were normal variables, they would only be available to that function based on scope rules, but modules seem to be global regardless of where you load them.
Now if you want to be able to download this json file from a web server, it will need to be in system.DocumentsDirectory instead of system.ResourceDirectory. Then you will need to add code to use network.request() to download the json file. You can check out my RSS Reader sample app to repurpose all the necessary network code from it. That example fetches XML from a URL, but as long as your json file is setting on a webserver somewhere (perhaps directly from dropbox if its a public file) then you should be good to go.

http://developer.anscamobile.com/code/rss-parser [import]uid: 19626 topic_id: 13975 reply_id: 62256[/import]

Rob,
I did init the table in line 32 but I forgot to add [j]. I’ll have a look at the rss app. [import]uid: 34126 topic_id: 13975 reply_id: 62265[/import]

Rob,

I init the itemData[j] = {} where you said and this is what the print gave me. I added a couple of other prints also for title and subtitle but they don’t print because the crash. What is it that I’m not doing correctly?

[code]
table: 0x602410 {
[1] => table: 0x602410 {
[admission] => “$20”
[time] => “21.00”
[month] => “October”
[place] => “The Big Bar”
[address] => “21 Down Street”
[description] => “Only hot chicks allowed…”
[date] => “1st”
}
[2] => table: 0x602410 {
[admission] => “$10”
[time] => “22.00”
[month] => “October”
[place] => “The Nice Bar”
[address] => “21 Nude Street”
[description] => “Only hot chicks allowed…”
[date] => “5th”
}
[3] => table: 0x602410 {
[admission] => “$10”
[time] => “22.00”
[month] => “October”
[place] => “The Nice Bar”
[address] => “21 Nude Street”
[description] => “Only hot chicks allowed…”
[date] => “5th”
}
[4] => table: 0x602410 {
[admission] => “$10”
[time] => “22.00”
[month] => “October”
[place] => “The Nice Bar”
[address] => “21 Nude Street”
[description] => “Only hot chicks allowed…”
[date] => “5th”
}
[5] => table: 0x602410 {
[admission] => “$10”
[time] => “22.00”
[month] => “October”
[place] => “The Nice Bar”
[address] => “21 Nude Street”
[description] => “Only hot chicks allowed…”
[date] => “5th”
}
[6] => table: 0x602410 {
[admission] => “$10”
[time] => “22.00”
[month] => “October”
[place] => “The Nice Bar”
[address] => “21 Nude Street”
[description] => “Only hot chicks allowed…”
[date] => “5th”
}
[7] => table: 0x602410 {
[admission] => “$20”
[time] => “21.00”
[month] => “November”
[place] => “The Big Bar”
[address] => “21 Drive Street”
[description] => “Only hot chicks allowed…”
[date] => “10th”
}
[8] => table: 0x602410 {
[admission] => “$20”
[time] => “21.00”
[month] => “November”
[place] => “The Big Bar”
[address] => “21 Drive Street”
[description] => “Only hot chicks allowed…”
[date] => “10th”
}
[9] => table: 0x602410 {
[admission] => “$20”
[time] => “21.00”
[month] => “November”
[place] => “The Big Bar”
[address] => “21 Drive Street”
[description] => “Only hot chicks allowed…”
[date] => “10th”
}
[10] => table: 0x602410 {
[admission] => “$20”
[time] => “21.00”
[month] => “November”
[place] => “The Big Bar”
[address] => “21 Drive Street”
[description] => “Only hot chicks allowed…”
[date] => “10th”
}
[11] => table: 0x602410 {
[admission] => “$10”
[time] => “22.00”
[month] => “November”
[place] => “The Nice Bar”
[address] => “21 Jump Street”
[description] => “Only hot chicks allowed…”
[date] => “15th”
}
}

Runtime error
…8ncjymmw0btj00000gn/T/TemporaryItems/212/screen2.lua:78: attempt to index global ‘itemData’ (a nil value)
stack traceback:
[C]: ?
…8ncjymmw0btj00000gn/T/TemporaryItems/212/screen2.lua:78: in function ‘new’
…60j8ncjymmw0btj00000gn/T/TemporaryItems/212/main.lua:67: in function ‘loadScreen’
…60j8ncjymmw0btj00000gn/T/TemporaryItems/212/main.lua:86: in function ‘_onRelease’
?: in function <?:1330>
?: in function <?:215>
[/code] [import]uid: 34126 topic_id: 13975 reply_id: 62323[/import]