[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

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'
0
RBS
Asked:
RBS
1 Solution
 
RBSAuthor Commented:
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
0
 
EE_AutoDeleterCommented:
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now