#is a type, not a valid in the given context

1 messages · Page 1 of 1 (latest)

hazy topaz
#

I have this code ... and it says "ConvertUserData.UserData" is a type, not a valid in the given context and I have no idea why I just grabbed this code straight from microsoft docs

using Newtonsoft.Json;

public class ConvertUserData
{
    public class UserData
    {
        public string type { get; set; }
        public string email { get; set; }
        public string password { get; set; }
    }

    public static string Convert(string _type, string _email, string _password)
    {
        var userData = new UserData
        {
            type = _type,
            email = _email,
            password = _password
        };
        return JsonSerializer.Serialize(UserData);

                              // ERROR HERE ⤴
    }
}
low depot
#

you're trying to serialize the type, not the instance

#

also why doesn't this use a constuctor?

hazy topaz
hazy topaz
low depot
#

ok hold up

#

looks like .Serialize() has changed since what you're following was published

#

it looks like it takes three arguments now

#

Serialize(Object, Type, JsonSerializerOptions)

#

I don't see it being used like that elsewhere though, so hmmm

#

maybe try JsonSerializer.Serialize<UserData>(userData)?

hazy topaz
#

thanks a lot for your time! i found this

#

somehow it works

low depot
#

Ohhh, you were using the wrong namespace!

#

the correct one is System.Text.Json

#

you were using the method from NewtonSoft.Json instead of System.Text.Json

#

which obviously takes different arguments, etc.

hazy topaz
#

yeah youre right lol

#

but I cant use System.Text.Json

#

and i cant find the package

#

nvm unity doesnt support it

#

thanks a lot thou!

low depot
#

Why not just use JsonUtility?