Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON parse error: Missing a name for object member

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.

like image 365
lawrencehagman Avatar asked Aug 09 '16 17:08

lawrencehagman


1 Answers

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!

like image 51
lawrencehagman Avatar answered Sep 20 '22 14:09

lawrencehagman