Home > Software engineering >  Newtonsoft.Json.JsonReaderException: Invalid JavaScript property identifier character: ,
Newtonsoft.Json.JsonReaderException: Invalid JavaScript property identifier character: ,

Time:12-02

I have this code

var list = new List<long>();
long id = 202;
list.Add(2000);
list.Add(2001);
list.Add(2002);
var stringOfIds = string.Join(",", list);
        
var paramList = @"{'ProjectId':"   id   ", 'EntityIDsList': "   stringOfIds   "}";
Console.WriteLine(paramList);
var parameters = JsonConvert.DeserializeObject<Dictionary<string, object>>(paramList);
Console.WriteLine(parameters);

for some particular reason, it doesn't Deserialize the object and it crashes. What I'm trying here to do is: transform a list of longs into a string, comma separated -> construct the paramList string and then deserialize it using Newtonsoft.Json. I believe that the error is somewhere in the stringOfIds but couldn't figure it out sadly. Do you know what am I doing wrong and how can I fix it?

CodePudding user response:

Right now your paramList looks like this:

{
    "ProjectId": 202,
    "EntityIDsList":
        2000,
        2001,
        2002
}

Which is not proper JSON. It should look like this:

{
    "ProjectId": 202,
    "EntityIDsList": [
        2000,
        2001,
        2002
    ]
}

So you should change it to:

var paramList = @"{'ProjectId':"   id   ", 'EntityIDsList': ["   stringOfIds   "]}";

Also at this point Console.WriteLine(parameters); won't tell you anything meaningfull, you should probably change it to Console.WriteLine(parameters.ToString());

CodePudding user response:

You have two "problems"

  1. you need to add extra single-quotes around the stringOfIds bit
  2. maybe it's actually what you want, but... this will give you a dictionary with 2 items with keys: "ProjectId" and "EnitityIDsList".

As the list is stringified you may as well use D<string, string> (or dynamic, depending on what you're actually trying to do.

I'm guessing you will want to have a collection of "projects"? So this form won't work for you.

[
  { "1": "1001,1002" },
  { "2": "2001,2002" }
]

is the normal json form for a dictionary of items

[
  { "1": [1001,1002] },
  { "2": [2001,2002] }
]

into a D<string,List<int>> would be "better".

Strongly suggest you create classes to represent the shapes and serialize those.

Also, although Newtonsoft will handle single quotes, they're not actually part of the spec. You should escape double-quotes into the string if you actually need to generate json this way.

Maybe this is just a cutdown snippet to demo your actual problem and I'm just stating the obvious :D The extra quotes is the actual "problem" with your sample code.

CodePudding user response:

The string you have, paramList is not a valid JSON. JSON object has keys (and values if they are strings) surrounded with double quotes, not single quotes.

Corrected string with escaped double quotes:

@"{""ProjectId"": "   id   @", ""EntityIDsList"": """   stringOfIds   @"""}";

If your purpose of writing this string is to convert it to an object, you should directly create an object. Also note that you cant print the objects with Console.WriteLine... you will need to convert this to a string first (JsonConvert.SerializeObject) then print it.

var parameters = new
{
    ProjectId = id,
    EntityIDsList = stringOfIds
};
Console.WriteLine(JsonConvert.SerializeObject(parameters, Formatting.Indented));

// output:
{
  "ProjectId": 202,
  "EntityIDsList": "2000,2001,2002"
}

If you want EntityIDList as a list of numbers, change the value of EntityIDsList to list instead of stringOfIds.

var parameters2 = new
{
    ProjectId = id,
    EntityIDsList = list
};
Console.WriteLine(JsonConvert.SerializeObject(parameters2, Formatting.Indented));

//output:
{
  "ProjectId": 202,
  "EntityIDsList": [
    2000,
    2001,
    2002
  ]
}
  • Related