Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am experimenting with JavaScriptSerializer to deserialize some JSON in C#, and have a couple of questions regarding the use of DataMember.

  1. I want my DataContract class to have a property called "Parts" that maps to a JSON object "rings". If I set the DataMember Name="rings" and name the property "Rings" everything works as expected. However, if I name the property "Parts" (leaving the DataMember Name="rings"). Parts is always null.

    // this is always null
    [DataMember(Name = "rings")]
    public ArrayList Parts { get; set; }
    
    
    // this works fine
    [DataMember(Name = "rings")]
    public ArrayList Rings { get; set; }
    
  2. Upon deserialization, is it possible to map multiple json objects to a single property. For example, the input json string may not contain "rings", but rather "point" or "line". Can I map all three types to the Parts property?

share|improve this question

2 Answers

JavaScriptSerializer is in System.Web.Extensions and does not know about DataMemberAttribute.

Try DataContractJsonSerializer which is in System.Runtime.Serialization.Json (.net 40 - System.Runtime.Serialization.dll, .net 3.5 - System.ServiceModel.Web.dll)

share|improve this answer

I recommend that you use some other JSON implementation for .NET. There are many of them open source that don't require changing classes. You simply have to pass your object and they know what to do.

share|improve this answer
Is there a particular JSON library that you would recommend? There seems to be quite a bit of them out there. – user163757 Mar 25 '10 at 16:28
You know of a magic mind reading JSON serialization library? Do tell... – Sky Sanders May 25 '10 at 13:22
check out the C# section at json.org – thelost May 25 '10 at 15:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.