JSON parsing error: missing name for object

I am new to json and trying to get a basic working example.

My http request returns {'username': '1'}, {'username': '1'}.

I am confused as to what actual json looks like, but also how to get it in a string variable for deserialization.

Since ToJson returns {'username': '1'}, I decided that I needed to do this in order to put it in double quotes in order to convert it back.

I am clearly missing something!

class DataItem{

    public string username;
}

string json = "{'username': '1'}";

deserialized = JsonUtility.FromJson<DataItem>(json);

Error: ArgumentException: JSON parsing error: missing name for an object member.

+4
source share
2 answers

, .

// 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);

1 '

, , !

+5

Source: https://habr.com/ru/post/1650702/


All Articles