Solved

deserializing  json in .net

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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