views:

455

answers:

3

Hello Guys,

I am unable to convert JSON string to .net object in asp.net. I am sending JSON string from client to server using hidden field (by keeping the JSON object.Tostring() in hidden field and reading the hidden field value in code behind file)

Json string/ Object:

 [[{"OfferId":"1","OrderValue":"11","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"11","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"11","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"2","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"2","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"67","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"67","HostingTypeID":"3"}],
[{"OfferId":"1","OrderValue":"99","HostingTypeID":"6"}],
[{"OfferId":"1","OrderValue":"10","HostingTypeID":"8"}]]

.Net Object

public class JsonFeaturedOffer
{
    public string OfferId { get; set; }

    public string OrderValue { get; set; }

    public string HostingTypeID { get; set; }
}

Converstion code in code behind file

byte[] byteArray = Encoding.ASCII.GetBytes(HdnJsonData.Value);
        MemoryStream stream = new MemoryStream(byteArray);
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonFeaturedOffer));
        object result= serializer.ReadObject(stream);
        JsonFeaturedOffer jsonObj = result as JsonFeaturedOffer;

While converting i am getting following error:

Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.

+1  A: 

If this is an array of arrays of JsonFeaturedOffers, shouldn't it be:

byte[] byteArray = Encoding.ASCII.GetBytes(HdnJsonData.Value);
MemoryStream stream = new MemoryStream(byteArray);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonFeaturedOffer[][]));
object result= serializer.ReadObject(stream);
JsonFeaturedOffer[][] jsonObj = result as JsonFeaturedOffer[][];
Nick Gotch
Thank you it worked for me..
Vinni
+1  A: 

If you want the class to auto-magically serialize into json/xml or deserialize in the object you need to decorate it with some serializable attributes:

[Serializable, XmlRoot("JsonFeaturedOffer"), DataContract(Name="JsonFeaturedOffer")]
public class JsonFeaturedOffer  
{
    [XmlElement ("OfferId"), DataMember(Name="OfferId")]
    public string OfferId {get; set;}

... and so on

amelvin
Thank you it helped me and problem got solved
Vinni
@Vinni - good news!
amelvin
+2  A: 

Instead of doing this manually I would recommend using the lightweight JavaScriptSerializer. No attributes are required on the classes you want to serialize/deserialize.

It's also more flexible and faster than the DataContractJsonSerializer, since it does not have to care about all the wcf stuff. Additionally it has generic overloads that make it very simple to use AND it can also handle anonymous types.

Serialization:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var objectAsJsonString = serializer.Serialize(objectToSerialize);

Deserialization:

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
JsonFeaturedOffer deserializedObject = derializer.Deserialize<JsonFeaturedOffer>(s_JsonBaseDate);

To make it even easier you can create Extension methods that will give you json serialization/deserialization directly on the objects/strings.

ntziolis