-2

my PHP file generate list of players like this :

["EnzoDeg40","EDeg40","AstroGeek"]

I have successfully downloaded the content with Unity Web Request. But I can't seem to find a solution to convert JSON to a simple array.

Enzo Degraeve
  • 165
  • 1
  • 1
  • 13
  • You can't find a solution? Do you mean that the serializer in Unity can't deserialize to a string array? Or? – ProgrammingLlama Nov 13 '21 at 12:53
  • 1
    IMHO, the JSON should be like `{ "someKey": [ "EnzoDeg40", "EDeg40", "AstroGeek" ] }`. Now we can create a class with `"someKey"` as its field and `JsonUtility.FromJson < TheClassCreated > ( jsonString )` will do the rest. – nIcE cOw Nov 13 '21 at 13:08

1 Answers1

2

You can use the JsonUtility. Note that "Unity does not support passing other types directly to the API, such as primitive types or arrays. If you need to convert those, wrap them in a class or struct of some sort.". You have to do something like this:

    [Serializable]
    public struct StringArrayWrapper { public string[] items; }

    private Start()
    {
            var arry = JsonUtility.FromJson<StringArrayWrapper>("{\"items\":[\"EnzoDeg40\",\"EDeg40\",\"AstroGeek\"]}")