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 ''.
JSON (JavaScript Object Notation) is THE standard design for human-readable data interchange. It is a light-weight, human-readable format for storing and transporting data. Since JSON works with a tree structure, it looks like XML. Due to its built-in features, it is very easy to comprehend and use.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.
Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.
Unfortunately, none of the proposed solutions solve the real source of the problem. This exception means that your deserializer tries to read from the end of a stream.
The solution is to rewind the stream to the beginning, ie. set the stream.Position = 0;
before deserialization.
Also, as the comments mention, if you used a StreamWriter you need to flush it before using the stream.
Instead of doing this manually I would recommend using the built in 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 = serializer.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With