I'm new to json and trying to get a basic example working.
My http request returns {'username': '1'},{'username': '1'}.
I'm confused as to what valid json looks like but also how to get it into a string variable to deserialize.
Since ToJson returns {'username': '1'}, I figured the right thing to do was to put it in double quotes to convert it back.
I'm obviously missing something!
class DataItem{
public string username;
}
string json = "{'username': '1'}";
deserialized = JsonUtility.FromJson<DataItem>(json);
Error: ArgumentException: JSON parse error: Missing a name for object member.
With very helpful responses I found what I was missing.
// Temp Data Struct
class DataItem{
public string username;
}
//Valid Json look like : {"username": "1"}
//Valid Json must be double quoted again when assigned to string var
// or escaped if you want 'valid' Json to be passed to the FromJson method
//string json = "{\"username\": \"1\"}"; or
string json = @"{""username"": ""1""}";
DataItem deserialized = JsonUtility.FromJson<DataItem>(json);
Debug.Log("Deserialized "+ deserialized.username);
Returns 'Deserialized 1'
Very basic stuff but thanks for helping me make sense of it!