96mg
|
  |
| Joined: 15 May 2017 |
| Total Posts: 775 |
|
|
| 23 Jul 2017 01:18 PM |
I want to convert a string to a table. I've got the table from a Website, and it it's kind of complicated.
The table consists of
Table > String
The String is the Actual Table.
Example : Test = [{"[{Hello},{WhatsUp}]"}]
So when I print : Test[1] it says : [{"[{Hello},{WhatsUp}]"}]
Is there a way to get the string "[{"[{Hello},{WhatsUp}]"}]" to be an Actual Table itself?
wunt s0m3 b0rk? |
|
|
| Report Abuse |
|
|
PLBH
|
  |
| Joined: 08 May 2014 |
| Total Posts: 151 |
|
|
| 23 Jul 2017 01:38 PM |
| table.insert(Test, 1, "[{Hello},{WhatsUp}]") ? |
|
|
| Report Abuse |
|
|
96mg
|
  |
| Joined: 15 May 2017 |
| Total Posts: 775 |
|
|
| 23 Jul 2017 01:41 PM |
I want to convert the string to a table, not insert it into one
wunt s0m3 b0rk? |
|
|
| Report Abuse |
|
|
PLBH
|
  |
| Joined: 08 May 2014 |
| Total Posts: 151 |
|
|
| 23 Jul 2017 01:45 PM |
Test = table.concat({" [{"[{Hello},{WhatsUp}]"}] "}) print(Test) |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2017 01:46 PM |
Just do what the first guy suggested
or if possible: http://wiki.roblox.com/index.php?title=API:Class/HttpService/JSONDecode
|
|
|
| Report Abuse |
|
|
96mg
|
  |
| Joined: 15 May 2017 |
| Total Posts: 775 |
|
|
| 23 Jul 2017 01:50 PM |
That's making the table a string. Im aiming for the opposite
wunt s0m3 b0rk? |
|
|
| Report Abuse |
|
|
LaeMVP
|
  |
| Joined: 24 Jun 2013 |
| Total Posts: 4416 |
|
|
| 23 Jul 2017 01:50 PM |
| If it is a JSON string you could just JSONDecode it |
|
|
| Report Abuse |
|
|
LaeMVP
|
  |
| Joined: 24 Jun 2013 |
| Total Posts: 4416 |
|
|
| 23 Jul 2017 01:51 PM |
| or i don't think it needs to be a json idk |
|
|
| Report Abuse |
|
|
96mg
|
  |
| Joined: 15 May 2017 |
| Total Posts: 775 |
|
|
| 23 Jul 2017 01:52 PM |
It is, and I've tried that, but It seems to act funky.
I'll try it again
wunt s0m3 b0rk? |
|
|
| Report Abuse |
|
|
BaiYuni
|
  |
| Joined: 09 Oct 2009 |
| Total Posts: 2861 |
|
|
| 23 Jul 2017 01:52 PM |
I'm not quite sure, but you can possibly get what's in the string and maybe you can create your own table?
Test = {"[\"Hello\"],[\"WhatsUp\"]"}
for tab in Test[1]:gmatch("%b[]") do table.insert(Test, tab) end
Test["Hello"] = {} Test["WhatsUp"] = {}
table.insert(Test["Hello"], "hiya") print(Test["Hello"][1])
Should correct me if I'm wrong on going about this. |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2017 01:52 PM |
Yeah, encoding/decoding JSON would work. That's the best way to go between a table and a string.
But if you have something really simple, like a comma-separated value list (CSV), you could do something like this:
local myStr = "Hello,Apple,Test,XYZ"
function ToString(tbl) return table.concat(tbl, ",") end
function ToTable(str) local tbl = {} for item in str:gmatch("[^,]+") do table.insert(tbl, item) end return tbl end
local myTbl = ToTable(myStr) -- Test: print("Same:", myStr == ToString(myTbl)) |
|
|
| Report Abuse |
|
|
|
| 23 Jul 2017 02:33 PM |
Alright, I just wrote this up now and traced is with the given example manually, so no guarantees. I also hope the order isn't important.
Test = "{{Hey, {Everyone}}, WhatsUp, {}, {{Hows, {It}, Going?}}}" result = {}
function ParseStringToTable(t,str) local mstr = str:match("^{(.*)}$") if mstr then -- should always pass, actually for w in mstr:gmatch("[^,]*") do if not w:match("[{}]") then t[#t+1] = w end end for w in mstr:gmatch("%b{}") do t[#t+1] = {} ParseStringToTable(t[#t],w) end end return t end
ParseStringToTable(result,Test)
--> result = {"WhatsUp", {"Hey", {"Everyone"} } , {""} , { { "Hows", "Going?", {"It"}}}}} |
|
|
| Report Abuse |
|
|