tosfera

fromJSON returning unexpected results

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.

Share this post


Link to post

Have you tried to loop through saveData to see whats in there?

Maybe its just the index thats causing problems.

Share this post


Link to post

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.

Share this post


Link to post
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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.