Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to define a class to deserialize the below Json Array



{"Errors":[{"Code":123,"Message":"Your account is Linked"}]}


Please help me on this

What I have tried:

I use the below class to Deserialize but getting the exception.

Public class ErrorDescription
{
public string Message {get;set;}
public int Code{get;set;}
}

public class Errors
{
public List<errordescription>;
}
Posted
Updated 1-Mar-18 1:26am

Use this website for generating classes from raw JSON: JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]

More tools and information can be found here: Working with JSON in C# & VB[^]
 
Share this answer
 
Hi, please go through the below link for your help.

[^]
 
Share this answer
 
In addition to Solution 1 and 2, try the below code which uses JavaScriptSerializer Class [^] for Deserialization,
Please add the reference System.Web.Extensions.dll
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace CPTemp
{
    public class Class1
    {
        static void Main(string[] args)
        {
            string input = "{\"Errors\":[{\"Code\":123,\"Message\":\"Your account is Linked\"}]}";
            JavaScriptSerializer js = new JavaScriptSerializer();
            ErrorInfo obj = js.Deserialize<ErrorInfo>(input);
            foreach (ErrorDescription item in obj.Errors)
            {
                string message = item.Message;
                int code = item.Code;
            }
        }
    }

    public class ErrorDescription
    {
        public string Message { get; set; }
        public int Code { get; set; }
    }

    public class ErrorInfo
    {
        public List<ErrorDescription> Errors { get; set; }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900