Solved

deserializing  json in .net

Posted on 2014-03-28
5
240 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 75

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 70

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 70

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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