Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:29:28+00:00 2026-05-27T00:29:28+00:00

Background: I’m invoking a web service method which returns a JSON string. This string

  • 0

Background:

I’m invoking a web service method which returns a JSON string. This string can be of type ASConInfo or ASErrorResponse.

Question:

Using the DataContractJsonSerializer, how can I convert the returned JSON string to one of those objects?

Thanks in advance

I have tried the following technique, but it does not work:

    public static object test(string inputString)
    {
        object obj = null;
        using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(inputString)))
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(object));
            obj = ser.ReadObject(ms) as object;
        }
        return obj;
    }

  [WebMethod]
    public string TypeChecker()
    {
        string str = "{\"Error\":191,\"ID\":\"112345678921212\",\"Length\":15}";
        //string strErro = "";

        object a = test(str);

        if (a is ASResponse)
        {
            return "ASResponse"; 
        }

        if (a is ASErrorResponse)
        {
            return "ASErrorResponse";
        }

        return "Nothing";
    }

[DataContract]   
public class ASResponse
{
    #region Attribut(s)
    private string idField;
    private int lengthField;
    private int callHomeField;
    private SlinkyRequest slinkyRequestField;
    private AssociationServeResponseMessage responseMessageField;
    private int errorField;
    private string slinkyIDField;
    private int tokenLengthField;
    private string tokenField;
    private SlinkyInfo slinkyInfoField;
    private string slinkyIPAddressField;
    private string videoSizeField;
    private string slinkyStreamingField;
    private int slinkyMaxFrameField;
    private int maxVideoRateField;
    private int maxAudioRateField;
    private AudioType audioTypeField;
    #endregion 

    #region Constructor(s)
    /// <summary>
    /// 
    /// </summary>
    public ASResponse()
    {
    }
    #endregion 

    #region Property(ies)
    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public string ID
    {
        get { return idField; }
        set { idField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>        
    public int Length
    {
        get 
        {
            return this.lengthField; 
        }
        set
        {
            this.lengthField = value;
        }
    }
    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public int CallHome
    {
        get { return callHomeField; }
        set 
        {
            if (this.callHomeField < StaticValues.callHomeMinValue || 
                this.callHomeField > StaticValues.callHomeMaxValue)
            {
                throw new ArgumentOutOfRangeException("CallHome value is out of Range.");
            }
            callHomeField = value; 
        }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public SlinkyRequest SlinkyRequest
    {
        get { return slinkyRequestField; }
        set { slinkyRequestField = value; }
    }


    public AssociationServeResponseMessage ResponseMessage
    {
        get { return responseMessageField; }
        set { responseMessageField = value; }
    }


    public int Error
    {
        get { return errorField; }
        set { errorField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public string SlinkyID
    {
        get { return slinkyIDField; }
        set { slinkyIDField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public int TokenLength
    {
        get { return tokenLengthField; }
        set { tokenLengthField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public string Token
    {
        get { return tokenField; }
        set { tokenField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public SlinkyInfo IsSlinkyInfoEnabled
    {
        get { return slinkyInfoField; }
        set { slinkyInfoField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public string SlinkyIPAddress
    {
        get { return slinkyIPAddressField; }
        set { slinkyIPAddressField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public string VideoSize
    {
        get { return videoSizeField; }
        set { videoSizeField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public string Streaming
    {
        get { return slinkyStreamingField; }
        set { slinkyStreamingField = value; }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public int MaxFrame
    {
        get
        {
            return slinkyMaxFrameField;
        }
        set
        {
            slinkyMaxFrameField = value;
        }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public int MaxVideoRate
    {
        get
        {
            return maxVideoRateField;
        }
        set
        {
            maxVideoRateField = value;
        }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public int MaxAudioRate
    {
        get
        {
            return maxAudioRateField;
        }
        set
        {
            maxAudioRateField = value;
        }
    }

    [DataMember]
    /// <summary>
    /// 
    /// </summary>
    public AudioType AudioType
    {
        get { return audioTypeField; }
        set { audioTypeField = value; }
    }

    /// <summary>
    /// 
    /// </summary>        

    public Dictionary<string, string> Extra_Metadata { get; set; }    
    #endregion 

}

/// <summary>
/// 
/// </summary>
[DataContract]   
public class ASErrorResponse
{
    /// <summary>
    /// 
    /// </summary>
    [DataMember]
    public int Length { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [DataMember]
    public string ID { get; set; }

    /// <summary>
    /// 
    /// </summary>
    [DataMember]
    public int Errorv2 { get; set; }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public static string FromASErrorResponseToJson(ASErrorResponse item)
    {
        string strJson = string.Empty;
        DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(ASErrorResponse));
        MemoryStream ms = new MemoryStream();
        dcjs.WriteObject(ms, item);
        strJson = Encoding.UTF8.GetString(ms.ToArray());
        return strJson;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:29:29+00:00Added an answer on May 27, 2026 at 12:29 am

    Not the best solution but this should at least work:

        public static object test(string inputString)
        {
            object obj = null;
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(inputString)))
            {
                try
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ASErrorResponse));
                    obj = ser.ReadObject(ms) as ASErrorResponse;
                }
                catch (SerializationException)
                {
    
                }
            }
    
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(inputString)))
            {
                try
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ASResponse));
                    obj = ser.ReadObject(ms) as ASResponse;
                }
                catch (SerializationException)
                {
    
                }
            }
    
            return obj;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background : I currently have a Web Forms, ASP.NET 3.5/C# application which I'm interested
Background I work for a large organization which has thousands of MS Access applications
Background I have a web app that will create an image from user input.
Background: My REST service project was started up by using Hibernate. I use id
Background I'm well into building a central appointment booking system for various service providers.
Background: Developing a trading system which subscribe to many events which are sent by
Background This is the same background as my previous question , except the Outline
Background: I have a MainTest class that has many buttons, each of which instantiate
Background Use Ajax to fire an event to the web server when a list
Background I first wanted to upload a file via json and get a response

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.