0

I have a json data:

"{\"list\":[{\"PlId\":1,\"PstId\":1,\"MonthlyValue\":\"00,00\"},{\"PlId\":2,\"PstId\":1,\"MonthlyValue\":\"00,00\"},{\"PlId\":3,\"PstId\":1,\"MonthlyValue\":\"00,00\"},{\"PlId\":4,\"PstId\":1,\"MonthlyValue\":\"00,00\"},{\"PlId\":5,\"PstId\":1,\"MonthlyValue\":\"00,00\"}]}"

I want to the json data convert to List but JsonConvert.Deserialize(jsonData) return null.

[Serializable]
public class DecryptedMonthlyPremiumScale
{
    [DataMember]
    public int PlId { get; set; }

    [DataMember]
    public int PstId { get; set; }

    [DataMember]
    public string MonthlyValue { get; set; }
}

I tried this method: How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?

What is wrong? Thanks.

Community
  • 1
  • 1
Salih KARAHAN
  • 299
  • 1
  • 4
  • 18

1 Answers1

1

You need to create a wrapper class to deserialize properly:

[Serializable]
public class DecryptedMonthlyPremiumScale
{
    [DataMember]
    public int PlId { get; set; }

    [DataMember]
    public int PstId { get; set; }

    [DataMember]
    public string MonthlyValue { get; set; }
}

public class Root
{
    public IList<DecryptedMonthlyPremiumScale> list {get;set;}
}

var obj = JsonConvert<Root>(json);

Another approach is to use JObject to get the root element and then deserialize:

var parsed = JObject.Parse(json)["list"].ToObject<IList<DecryptedMonthlyPremiumScale>>();
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50