0

can't seem to find an elegant way to deal with this!

lets say i have a method that returns a json array like this ["3","4","5"] as a string in Unity

how do i read or work with each item (3, 4, and 5) individually? (with each item as an integer, not string)

coiny123
  • 1
  • 1

1 Answers1

0

hey all i found my preferred simple solution

public string[] arrayOfString;

string response = "["1","2","3"]" // alternatively you would get this when json array returned ["1","2","3"]

response = response.Trim('[', ']'); // "1","2","3"
response = response.Replace("\"", ""); // 1,2,3

arrayOfString = response.Split(','); // becomes an array of string

arrayOfString[0] // returns 1

for array of Int from String

public Int[] arrayOfInt;

string response = "["1","2","3"]" // alternatively you would get this when json array returned ["1","2","3"]

response = response.Trim('[', ']'); // "1","2","3"
response = response.Replace("\"", ""); // 1,2,3

arrayOfInt = Array.ConvertAll(response.Split(','), int.Parse); // to create array of integers from string

arrayOfInt[0] // returns 1

coiny123
  • 1
  • 1
  • If you are going to use JSON for more than just one array you should definitely use a library to serialize/deserialize JSON in C# like: https://www.newtonsoft.com/json – OOM May 27 '22 at 14:29