-1

have a data like this

result = { 
    [1] = { ["identifier"] = MMK18495,["vehicles"] = {"vehN":"Caracara 4x4","vehM":"caracara2","totals":3},["id"] = 1,} ,
    [2] = { ["identifier"] = MMK18495,["vehicles"] = {"vehN":"Sandking SWB","vehM":"sandking2","totals":3},["id"] = 2,} ,
    [3] = { ["identifier"] = MMK18495,["vehicles"] = {"totals":5,"vehN":"Caracara 4x4","vehM":"caracara2"},["id"] = 3,} ,
    }

trying to sort this data to a menu like this

    for i=1, #result, 1 do
        local ownedcars = result[i].vehicles
        print(dump(ownedcars))
        for _,v in pairs(ownedcars) do  -- <- the error is here
            menu[#menu+1] = {
                header = " Model "..v.vehM.." Name "..v.vehN.." quantity"..v.totals,
                txt = "",
            }
        end
    end

the output of ownedcars

{"vehN":"Caracara 4x4","vehM":"caracara2","totals":3}

but here is the error enter image description here

3
  • 1
    That is not a Lua table, looks like you have a string containing a json structure Commented Jan 3, 2023 at 1:51
  • this is LUA forget about menu table, how to sort vehicles for each ID ? As a result, I would like to receive the following text. id 1 vehicles vehM totals ``` vehM caracara2 - totals 3 vehM sandking2 - totals 3 vehM caracara2 - totals 5 ``` Commented Jan 3, 2023 at 15:31
  • You would need to decode the json string. Commented Jan 3, 2023 at 17:15

1 Answer 1

0

As others have commented, this is not a Lua table. When inputting JSON tables, Lua reads them as strings. You will first need to convert your given JSON string into Lua. Thankfully, others have already done this. I will refer you to this question which has answers that solved this problem.

Once you've converted your JSON string to a Lua table, you should be good to go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.