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 \":\"incom plete\",\" cmi.core.t otal_time\ ":\"00:55: 38.90\"}"
I have created a class called SCO that looks like this:
In my Web Service, I attempt to deserialize the JSON string as follows:
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.co re.", "Cmi_core_");
That recults in a string Data_Value that looks like this:
"{\"Cmi_core_lesson_status \":\"incom plete\","C mi_core_to tal_time\" :\"00:55:3 8.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'
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
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; }
}
}
}
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;
}
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.co
That recults in a string Data_Value that looks like this:
"{\"Cmi_core_lesson_status
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'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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