I’ve come up on h** opefully** the last problem I face with this project and I need some help. I have a JSON databse that looks like this called ZIPDATABASE.json;
[{ "Zipcode": 98807, "City": "WENATCHEE", "State": "WA" }, { "Zipcode": 98811, "City": "ARDENVOIR", "State": "WA" }, { "Zipcode": 98812, "City": "BREWSTER", "State": "WA" },]
To call back to it I use the following code;
local fr = io.open(system.pathForFile("ZIPDATABASE.json")) local data = fr:read('\*a') fr:close() data = json.decode(data) local dbtbl = {} local record for i=1, #data do record = data[i] dbtbl[record["Zipcode"]] = { city = record.City, state = record.State } end
Everything works great except for one problem.
I’m trying to get it so when someone types in a text field the zip code for example “98811” after the phase has ended the state and city are auto-filled into the other fields. so currently the code looks like this;
local function zipListener( event ) if ( "began" == event.phase ) then elseif ( "editing" == event.phase ) then zipString = event.target.text elseif ( event.phase == "ended" or event.phase == "submitted" ) then zipString = event.target.text stateString = dbtbl[zipString].state cityString = dbtbl[zipString].city cityField.text = dbtbl[zipString].city stateField.text = dbtbl[zipString].state native.setKeyboardFocus( nil ) end end -- Create text field zipField = native.newTextField( 150, 150, 140, 15 ) zipField.isEditable = true zipField:addEventListener( "userInput", zipListener ) zipField.x = display.contentCenterX - 105 zipField.y = display.contentCenterY + 115 sceneGroup:insert(zipField)
This however gives me an error "attempt to index field ‘?’ (a nil value). Now if I were to replace the dbtbl[zipString].state with an actual zipcode for example dbtbl[98811].state then it works no problem. I cant, however, write separate code for every zipcode I’m using (which is the entire USA).
Is there a way for me to substitute the “98811” in dbtbl[98811].state with the string the user types out so that whatever zip they type in will show the corresponding state/city?
Thank you!!
-B