Solved

deserializing  json in .net

Posted on 2014-03-28
5
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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