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

  • SEARCH
  • Home
  • 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 7406943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:38:00+00:00 2026-05-29T05:38:00+00:00

My first attempt and JSON desrialization and I’m stuck, just wondering if you could

  • 0

My first attempt and JSON desrialization and I’m stuck, just wondering if you could help?

I have the following JSON

{
    "summary":{
        "pricing":{
            "net":988,
            "tax":13,
            "gross":729
        },
        "status":{
            "runningfor":29881175,
            "stoppedfor":88805,
            "idlefor":1298331744
        }
    }
}

here my c# code

private void MakeRequest()
{
    HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest; 
    request.Method = "GET";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Credentials = new NetworkCredential(Username, Password);
    request.Headers.Add(string.Format("App-Key: {0}", ApiKey));

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string resutls = reader.ReadToEnd();
        Response.Write(resutls);
        Status status = JSONHelper.JsonDeserialize<Status>(resutls);
        Response.Write(status.RunningFor);
    }
}

public class JSONHelper
{
    /// <summary>
    /// JSON Deserialization
    /// </summary>
    public static T JsonDeserialize<T>(string jsonString)
    {
        T obj = Activator.CreateInstance<T>();
        MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
        obj = (T)serializer.ReadObject(ms);
        ms.Close();
        return obj;
    }
}


[DataContractAttribute(Name = "status")]
public class Status
{
    [DataMember(Name = "runningfor")]
    public int RunningFor{ get; set; }
    [DataMember(Name = "stoppedfor")]
    public int StoppedFor{ get; set; }
    [DataMember(Name = "idlefor")]
    public int IdleFor{ get; set; }
}

And I’m only interested in the status result nothing else at all. what am I doing wrong as it is only returning 0 for RunningFor.

Thanks In advance

  • 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-29T05:38:02+00:00Added an answer on May 29, 2026 at 5:38 am

    You need to deserialize into a structure which maps to the whole JSON you’re trying to deserialize, not only to the part you want. In your case, the code below shows one way of doing it.

    public class StackOverflow_9135439
    {
        const string JSON = @"{
        ""summary"":{
            ""pricing"":{
                ""net"":988,
                ""tax"":13,
                ""gross"":729
            },
            ""status"":{
                ""runningfor"":29881175,
                ""stoppedfor"":88805,
                ""idlefor"":1298331744
            }
        }
    }";
        [DataContractAttribute(Name = "status")]
        public class Status
        {
            [DataMember(Name = "runningfor")]
            public int RunningFor { get; set; }
            [DataMember(Name = "stoppedfor")]
            public int StoppedFor { get; set; }
            [DataMember(Name = "idlefor")]
            public int IdleFor { get; set; }
        }
    
        [DataContract]
        public class Summary
        {
            [DataMember(Name = "status")]
            public Status Status { get; set; }
    
            // add "pricing" later if you need
        }
    
        [DataContract]
        public class Response
        {
            [DataMember(Name = "summary")]
            public Summary Summary { get; set; }
        }
    
        public class JSONHelper
        {
            /// <summary>
            /// JSON Deserialization
            /// </summary>
            public static T JsonDeserialize<T>(string jsonString)
            {
                T obj = Activator.CreateInstance<T>();
                MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms);
                ms.Close();
                return obj;
            }
        }
    
        public static void Test()
        {
            Response resp = JSONHelper.JsonDeserialize<Response>(JSON);
            Console.WriteLine(resp.Summary.Status.RunningFor);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so this is my first attempt at doing anything with JSON. I have
Consider my first attempt, a simple type in F# like the following: type Test()
This is my first attempt to build a json webservice using the Tornado Framework
I am trying to attempt my first Google Chrome Extension and have a question.
First attempt to use this cool site - after searching for 2 hours: So
First day and first attempt at using Scala - so go easy on me!
This is my first attempt at responsive design, so I'm keeping it simple. I
This is my first attempt to write shorthand if statements however am befuddled by
Because this is my first attempt at an extension method that seems quite useful
this if my first attempt at using streaming for WCF, and I am struggling

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.