Solved

Deserialize the following in c#

Posted on 2014-04-08
4
150 Views
Last Modified: 2014-04-26
I am new to deserialization using JSON, so I need to know how to create a class object that gets assigned the values from the deserialization process.  I have the following data retrieved in JSON that I would like to properly create a class and be able to throw that information into the created class.  Please help!  Thanks!

{
    "response":
     {
                  "authToken":  "webapi123431095"
     },
    "status":
     {
              "detail":
               {
               },
               "success": true
     }
}

Open in new window

0
Comment
Question by:VBBRett
  • 2
4 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 250 total points
ID: 39986799
I think this had already been addresses in a previous question :)

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28268323.html
0
 

Author Comment

by:VBBRett
ID: 39986867
Thanks, but since I am new to deserializing, do you know of a way to deserialize the string that I posted?  Thanks!
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39987463
There are plenty of examples on how to do this. For instance, my comment here:

http://www.experts-exchange.com/Programming/Languages/.NET/Q_28400081.html#a39962468

The basic idea is that anywhere in your JSON that you have curly braces ( {} ), you will be creating a new class; anywhere in your JSON that you have square brackets ( [] ), you will be creating an array; everything else is just a property. You need to create a "root" class to hold the overall JSON once it's converted.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39987472
In your case, you might define a root class as:

public class ServiceResponse
{
}

Open in new window


...which will have two properties:

public class ServiceResponseModel
{
    public XXXX Response { get; set; }
    pulbic YYYY Status { get; set; }
}

Open in new window


Now, both response and status are followed by curly braces, so that means you need a new class for each:

public class ResponseModel
{
    public string AuthToken { get; set; }
}

public class StatusModel
{
    ZZZZ  // I'll let you fill in ZZZZ  = )
}

Open in new window


These new classes will be the types for the two new properties:

public class ServiceResponseModel
{
    public ResponseModel Response { get; set; }
    pulbic StatusModel Status { get; set; }
}

Open in new window


Once you fill in everything, it's just a matter of passing the data and the type information (i.e. ServiceResponseModel) to the serializer.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now