?
Solved

JSON Deserializer to Class with property names that have periods

Posted on 2011-10-19
4
Medium Priority
?
626 Views
Last Modified: 2012-06-21
Hi:

I have the following JSON string that I am trying to deserialize:

{"cmi.core.lesson_status":"incomplete"}

I am trying to deserialize this in the following manner:

 
JavaScriptSerializer ser = new JavaScriptSerializer();
SCO sco = ser.Deserialize <SCO>(data_value);

Open in new window


My difficulty is making the SCO class:

When I go to create the class - i.e.

 
private string cmi.core.lesson_status;

public string Cmi.Core.LessonStatus
 {
     get { return cmi.core.lesson_status; }
     set { cmi.core.lesson_status = value; }
 }

Open in new window


I get an error telling me that there is no namespace core...

Any suggestions on how I could handle this greatly appreciated.

RBS
0
Comment
Question by:RBS
  • 3
4 Comments
 
LVL 20

Accepted Solution

by:
ddayx10 earned 2000 total points
ID: 36996603
Looks like your trying to create a class within a particular namespace. The syntax you are using is not going to compile (as you already know).

You'd want to do something like the following(see snippet).

You may want to do a search on c# namespace, or .net namespace to get a little more info.


namespace cmi.core
{
    public class SomeClass
    {
        //This would be common practice for a "property" in a class
        //c# conventions past 3.0 (I believe) make this part superfluous.
        private string lesson_status;

        //This is the actual property
        public string LessonStatus
        {
            get { return lesson_status; }
            set { lesson_status = value; }
        }

        //This would be a default constructor
        public SomeClass()
        { }
    }
}

Open in new window

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 36996627
PS: You likely also need to make the class "serializable"...

Info.... http://msdn.microsoft.com/en-us/library/system.serializableattribute%28v=VS.90%29.aspx

namespace cmi.core
{
    [Serializable()]
    public class SomeClass
    {
        //This would be common practice for a "property" in a class
        //c# conventions past 3.0 (I believe) make this part superfluous.
        private string lesson_status;

        //This is the actual property
        public string LessonStatus
        {
            get { return lesson_status; }
            set { lesson_status = value; }
        }

        //This would be a default constructor
        public SomeClass()
        { }
    }
}
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 36996676
Just for the heck of it I'll throw this out there, you could simplify your property creation by doing the following. In c# 3.0 and higher the convention has been simplified due to common practice.

<see snippet>
namespace cmi.core
{
    [Serializable()] 
    public class SCO
    {
        //This would be common practice for a "property" in a class
        //c# conventions past 3.0 (I believe) make this part superfluous.
        //private string lesson_status;

        //This is the actual property
        public string LessonStatus
        {
            get;
            set;
        }

        //This would be a default constructor
        public SCO()
        { }
    }
}

Open in new window

0
 

Author Closing Comment

by:RBS
ID: 36997492
thanks!

rbs
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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