Solved

deserializing  json in .net

Posted on 2014-03-28
5
235 Views
Last Modified: 2014-03-28
I have this Json response:
Dim json As String = "{
            'ResponseIG' :{
               'Email': 'james@example.com',
               'Active': true,
               'Roles': [
                       'User',
                       'Admin'
               ]
             }
 }"

I am trying to create the classes to  read this Json string   and  I'm using
JsonConvert.DeserializeObject  to do it

Can anyone help with the classes and how do i use JsonConvert.DeserializeObject to read the data?

I am having issues figuring out how to get inside the  'ResponseIG'   level to read the rest of the data.

This is the classes that i created:

 Public Class Rootobject
        Public Property ResponseIG As Responseig
    End Class

    Public Class Responseig
        Public Property Email As String
        Public Property Active As Boolean
        Public Property CreatedDate As Date
        Public Property Roles() As String
    End Class

And i making this call to get the data
Dim response As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(json)

But when i looked at the response object it is empty.

Thanks
0
Comment
Question by:chicho12
  • 2
  • 2
5 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39962468
Create a new class:

public class TheRoot
{
}

Open in new window


Name it whatever you like. Within this new class, add a property with name ResponseIG:

public class TheRoot
{
    public XXXX ResponseIG { get; set; }
}

Open in new window


Now you need a new class to represent the XXXX:

public class ResponseIGClass
{
}

Open in new window


Again, name it whatever you like. Replace the XXXX now:

public class TheRoot
{
    public ResponseIGClass ResponseIG { get; set; }
}

Open in new window


Now you need to add the remaining properties to the sub-class:

public class ResponseIGClass
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public string[] Roles { get; set; }
}

Open in new window


Do note that your Roles property is an array in the JSON, so it needs to be an array in the .NET code.

Once you have all of that put together, you should be able to deserialize:

string json = "the JSON";
TheRoot instance = JsonConvert.DeserializeObject<TheRoot>(json);

Open in new window

0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39962504
just a small problem.

just change:
Public Property Roles() As String
for
Public Property Roles As String()


also check http://emoreau.com/Entries/Articles/2014/01/Using-JsonNet.aspx
0
 

Author Comment

by:chicho12
ID: 39962516
Thanks for your answer.
i followed your steps, but the property ResponseIG of the instance object is still nothing after runnin this line:
TheRoot instance = JsonConvert.DeserializeObject<TheRoot>(json);

Anything else i might be missing??

thanks
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 39962525
have you tried my solution? only change the () and it is working!
0
 

Author Comment

by:chicho12
ID: 39962548
Thanks Eric. That worked !!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

18 Experts available now in Live!

Get 1:1 Help Now