Jump to content

fromJSON returning unexpected results


tosfera

Recommended Posts

Alright, I've got the following JSON saved in my mysql:

 

[ [ { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 } ], [ { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 } ] , [ { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 } ] ]

It are 6 elements inside 3 elements. Each of these 3 elements holds 6x the { "lastPlayed": 0, "experience": 0, "money": 20 }.

Whenever I'm using fromJSON, it's returning the wrong information when I'm trying to access the first element out of the second collection. The code I'm using is as following:

local saveData = fromJSON ( data [ 1 ].saves );
outputChatBox ( #saveData ); -- returns 6 elements (incorrect)
outputChatBox ( #saveData [ 2 ] ); -- returns 3 properties (correct)
outputChatBox ( tostring ( saveData [ 2 ] [ 1 ] ) ); -- returns nil

 

The JSON is valid, I can use it in different languages too and every parser says it's correct. I'm starting to think that fromJSON is returning just the first collection instead of all 3. Has anyone encountered this problem? I'm not really a fan of putting it all into 1 collection, it's preventing me from adding more and more data later on.

Link to comment

If I'm looping through the saveData, it's already returning 6 things. which is actually incorrect since it should return 3 collections. Looping through the saveData like this also returns the wrong collection:

for i, p in ipairs ( saveData ) do
	outputChatBox ( i .."--".. tostring ( p ) );
end

 

I'll just do it the ugly way since the function isn't going deep enough.

Link to comment
3
6
table: 0063BDA0

....

i think you should check  data [ 1 ].saves

 

Code i used:

Spoiler

I touched only the JSON:decode since i were not on MTA and fromJSON does not exist.


JSON = assert(loadfile "JSON.lua")()
function main()
	local saveData = JSON:decode('[ [ { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 } ], [ { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 } ] , [ { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 }, { "lastPlayed": 0, "experience": 0, "money": 20 } ] ]')
	
	outputChatBox ( #saveData ); -- returns 6 elements (incorrect)
	outputChatBox ( #saveData [ 2 ] ); -- returns 3 properties (correct)
	outputChatBox ( tostring ( saveData [ 2 ] [ 1 ] ) ); -- returns nil
end

function outputChatBox( text )
	print(text)
end

main()

 

Edited by LoPollo
Added the code i've used
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...