C
C#10mo ago
SinAw

✅ problem

I call a function that returns something like this
c#
"["value 2","value 1"]"
c#
"["value 2","value 1"]"
How can I turn this into a Array ?
18 Replies
cap5lut
cap5lut10mo ago
so how do u print this string? so give us some basic information (the code u showed earlier as well)
SinAw
SinAwOP10mo ago
this is a http request to lua function C#
c#
mta = new MTA("5.42.223.36", 22005, "user", "pass");
MTA_LuaArgs args = new MTA_LuaArgs();
var item = mta.CallFunction("discord_bot", "getInfo", args);
await command.RespondAsync(item);
c#
mta = new MTA("5.42.223.36", 22005, "user", "pass");
MTA_LuaArgs args = new MTA_LuaArgs();
var item = mta.CallFunction("discord_bot", "getInfo", args);
await command.RespondAsync(item);
Lua
local originalGetPlayerCount = getPlayerCount
function getInfo()
return getServerName() , "Player Count : "..tonumber(originalGetPlayerCount())
end
local originalGetPlayerCount = getPlayerCount
function getInfo()
return getServerName() , "Player Count : "..tonumber(originalGetPlayerCount())
end
Angius
Angius10mo ago
I would start by questioning why a method returns this string in the first place and not an array
cap5lut
cap5lut10mo ago
because, i might not be able to help ya, so if anyone else sees this thread they might be able to help if they see the whole thing
Angius
Angius10mo ago
What do you mean "http request to a LUA function"? I see no HTTP requests here
cap5lut
cap5lut10mo ago
we were in #chat first
Angius
Angius10mo ago
Ah, I lack context then, carry on
cap5lut
cap5lut10mo ago
now all context is given this string, how u do u print it? thats not given from the shown code or do u see it via debugger?
SinAw
SinAwOP10mo ago
I actually see ["Value 1","Value 2"] I just wanted to say that this is the type of string
cap5lut
cap5lut10mo ago
if its ["Value 1","Value 2"] (which is valid json, a list with 2 string elements), instead of "["Value 1","Value 2"]" which is invalid json, it makes quite a difference u r using Newtonsoft.Json, right?
SinAw
SinAwOP10mo ago
yes
cap5lut
cap5lut10mo ago
and u want to deserialize it to a string array? as in string[]? because that would be a simple string[] arr = JsonConvert.DeserializeObject<string[]>(content); what type is item btw?
SinAw
SinAwOP10mo ago
ow it worked, thank you very much
cap5lut
cap5lut10mo ago
if it worked, then this ⤴️ with the extra " at the start and the end was just about how u displayed the content and if u r on .NEt 7 or later u should really look into using System.Text.Json instead, its since .net 7 the defacto standard unless u have some niche cases regarding invalid json @- SinAw if ur question is answered, please mark this thread as that by $close -ing it
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
SinAw
SinAwOP10mo ago
ok i use it , thank you very much $close -ing
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server