Solved

deserializing  json in .net

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

943 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

8 Experts available now in Live!

Get 1:1 Help Now