Link to home
Start Free TrialLog in
Avatar of RBS
RBS

asked on

Deserialization Blues

Hi:

I have a JSON string that I am trying to deserialize into an object that I have in a Web Service.

The string comes into my services as parameter string data_value and looks like this:

"{\"cmi.core.lesson_status\":\"incomplete\",\"cmi.core.total_time\":\"00:55:38.90\"}"

I have created a class called SCO that looks like this:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for SCO2
/// </summary>
/// 
namespace cmi.core
{
    public class SCO2
    {
        public SCO2()
        {
        }

        private string lesson_status;
        private string total_time;

        public string Lesson_status
        {
            get { return lesson_status; }
            set { lesson_status = value; }
        }

        public string Total_time  //(
        {
            get { return total_time; }
            set { total_time = value; }
        }
    }

}

Open in new window


In my Web Service, I attempt to deserialize the JSON string as follows:

 
JavaScriptSerializer ser = new JavaScriptSerializer();
        int lesson_status = 0;
        try
        {
            SCO sco  = ser.Deserialize<SCO>(Data_Value);

         }
        catch (Exception er)
        {
            string ErrorMessage = er.Message;
        }

Open in new window


The procedure works fine except that the name value pairs in the JSON string don't match the class properties that I have created.
The problem is that in the name value pairs in the JSON string, the names have periods in them i.e.  cmi.core.something....

I've tried various methods by changing the namespace etc. in the class but to no avail.

Finally, I thought that I would take out the periods in the JSON string with:  string Data_Value = data_value.Replace("cmi.core.", "Cmi_core_");

That recults in a string Data_Value that looks like this:


"{\"Cmi_core_lesson_status\":\"incomplete\","Cmi_core_total_time\":\"00:55:38.90\"}"

It's exactly the same as the original string with underscores instead of periods.

However, when I use Data_Value as an input instead of data_value, I get the error message:


"Type 'System.String' is not supported for deserialization of an array."

I'm not sure what to do at this point.  Any suggestions greatly appreciated.

RBS

I'
Avatar of RBS
RBS

ASKER

OK - so I figured it out - changed the elements in the class from

public string Total_time  //(
        {
            get { return total_time; }
            set { total_time = value; }
        }

to:
public string Total_time  { get; set; }

Why this worked, I have no idea?  Can any one suggest why?

RBS
ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial