Home > Net >  JSON deserialize throws exception
JSON deserialize throws exception

Time:06-16

I have a C# code that defines a constant JSON string and a corresponding POCO class. however i get an exception:

The JSON value could not be converted Path: $ | LineNumber: 0 | BytePositionInLine: 1.

Code

try
{
    var filters = JsonSerializer.Deserialize <CmsContactsFilter>(FilterJson);
}
catch(Exception ex)
{

}

JSON

@"[{""cms"":""us-its"",""group"":[""ciso"",""cloudAdminsDistributionList"",""cloudAdmins""]},""cms"":""us-csdaudit"",""abc"":[""biso"",""costManagement""]},]";

POCO Class

public class CmsContactsFilter
{
    public string Cms { get; set; }
    public List<string> Group { get; set; }
}

CodePudding user response:

Your json is not valid, here is the valid version of your json:

[
    {
        "cms": "us-its",
        "group": [
            "ciso",
            "cloudAdminsDistributionList",
            "cloudAdmins"
        ]
    },
    {
        "cms": "us-csdaudit",
        "group": [
            "biso",
            "costManagement"
        ]
    }
]

Your code should be looks like this:

using System.Text.Json;

string filterJson = System.IO.File.ReadAllText("data.json");

var filters = JsonSerializer.Deserialize<List<CmsContactsFilter>>(filterJson);

foreach (var item in filters)
{
    Console.WriteLine(item.cms ":");
    foreach (var y in item.group)
    {
        Console.WriteLine("----" y);
    }
}
public class CmsContactsFilter
{
    public string cms { get; set; }
    public List<string> group { get; set; }

}

the name of the properties of the the CmsContactsFilter should be same with your json attributes names. if they are in lower-case format, your attribute name in the C# should be in the lower-case too.

CodePudding user response:

your json is not valid, you need to add { to the second array item, and use an object collection for deserialization

var FilterJson = @"[{""cms"":""us-its"",""group"":[""ciso"",""cloudAdminsDistributionList"",""cloudAdmins""]},{""cms"":""us-csdaudit"",""abc"":[""biso"",""costManagement""]}]";

var filters = System.Text.Json.JsonSerializer.Deserialize <List<CmsContactsFilter>>(FilterJson);

and fix class

public class CmsContactsFilter
{
    public string cms { get; set; }
    public List<string> group { get; set; }
    public List<string> abc { get; set; }
}

but as I understand your array has much more objects then 2. So you can try this code for the big json with different array names (if you had used Newtonsoft.Json this code could be significanly simplier)

   JsonArray array = JsonNode.Parse(FilterJson).AsArray();
    List<CmsContactsFilter> filters = new List<CmsContactsFilter>();
    foreach (JsonObject item in array)
    {
        var obj = new CmsContactsFilter();
        foreach (var prom in item.AsObject())
        {
            var name = prom.Key;
            if (name == "cms")
            {
                obj.cms = prom.Value.ToString();
                continue;
            }
            obj.groupName = name;
            obj.group = System.Text.Json.JsonSerializer.Deserialize<List<string>>(prom.Value.ToString());
        }
        filters.Add(obj);
    }
}

class

public class CmsContactsFilter
{
    public string cms { get; set; }
    public string groupName { get; set; }
    public List<string> group { get; set; }
}
  • Related