开发者

Error converting JSON to .Net object in asp.net

开发者 https://www.devze.com 2022-12-27 15:27 出处:网络
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 h

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(HdnJson开发者_如何学PythonData.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 ''.


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


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[][];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号