[Resolved] reading json and converting table values

Hey guys. I’ve loaded a json file and tossed it into a table. I’ve been successful in using the values as i pleased. But I’ve been hardcoding the values in the table.

local myBadgeArray = {  
 myTable[2].badge,  
 myTable[3].badge,  
 myTable[4].badge,  
 myTable[5].badge,  
 myTable[6].badge  
}  

However, Im trying to write some lua to just read the json values itself since i dont know how long the json is going to get. I ended up with something like this…

local myBadgeArray = { }  
  
tValS = "myTable["  
tValE = "]."  
tValB = "badge"  
   
for k, v in pairs(myTable) do  
 if k==1 then  
 table.insert(featureTable,k,tValS..k..tValE..tValB..",")  
  
 else  
 table.insert(myBadgeArray,k,tValS..k..tValE..tValB..",")  
 print(myBadgeArray[k])  
 --print(#myBadgeArray)   
 end  
end  

So that prints out exactly like the hardcoded table (myTable[2].badge and so on…) Which on the json file points to “g_bug_10off.png”, etc.

then I have another loop to take these table values and format them as i like…

for i,v in pairs(myBadgeArray) do  
 myBadges[i] = display.newImage("images/"..v,0,120)  
 myBadges[i]:translate(100,(i-1) \* 102)  
 myBadges[i]:scale(.75,.75)  
 myBadges[i]:setReferencePoint(display.CenterReferencePoint);  
 myBadges[i]:addEventListener("tap", ImageClicked)  
 coupGroup:insert(myBadges[i])  
end  

Okay… Here is where im getting confused. When I run this with the hardcoded table, my lua finds the images no problemo. But when i run the table insert script, instead of looking for “g_bug_10off.png”, its looking for “myTable[2].badge” in my images folder!?

Anyone know what im doing wrong here? I have a feeling im over thinking this but was just wondering if i missed something

Any help would be awesome! Thanks guys!
-Kel [import]uid: 88004 topic_id: 27463 reply_id: 327463[/import]

I think it has to do with taking the json values and moving it into another table, breaking the association with the json? [import]uid: 88004 topic_id: 27463 reply_id: 111587[/import]

Err yep. Just had to rewrite. I was totally over thinking it! lol

[code]

myBadges = {}

for k, v in pairs(myTable) do
table.insert(myBadges,myTable[k])
print(myBadges[k].badge)

badge = display.newImage(“images/”…myBadges[k].badge,0,120)

end

[/code] [import]uid: 88004 topic_id: 27463 reply_id: 111593[/import]

Hah, nicely done :slight_smile: Thanks for updating with your solution, that’s always really helpful for others who get stuck.

Peach :slight_smile: [import]uid: 52491 topic_id: 27463 reply_id: 111630[/import]