Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid JSON primitive when deserializing

Tags:

json

c#

Alright, so having this issue when I try to run my application:

Invalid JSON primitive: .

public static void ReloadConfig()
{
    if (!File.Exists("config.cfg"))
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("{\r\n");
        sb.Append("\"Admins\":[76561198214617172],\r\n");
        sb.Append("\"Chatty\":false,\r\n");
        sb.Append("}");

        File.WriteAllText("config.cfg", sb.ToString());
    }
    try
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        config = jss.Deserialize<Roles>(File.ReadAllText("config.cfg"));
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadKey();
        ReloadConfig();
    }
}

And this is how the config looks like when it's generated:

{
"Admins":[76561198214617172],
"Chatty":false,
}

As from my error message I would assume it says I have a space in my config, but I do not have that.

And if that matters I use System.Web.Script.Serialization.

like image 426
Joakim Carlsson Avatar asked Apr 29 '15 12:04

Joakim Carlsson


2 Answers

You have an errant comma in your output after false:

{
"Admins":[76561198214617172],
"Chatty":false,
}

Should be:

{
"Admins":[76561198214617172],
"Chatty":false
}
like image 122
DavidG Avatar answered Nov 20 '22 02:11

DavidG


You need to remove the comma after false:

{
"Admins":[76561198214617172],
"Chatty":false
}

I used JSONLint to validate your JSON, there are many other online JSON validators.

like image 33
Alex Avatar answered Nov 20 '22 01:11

Alex