Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

c# - JavaScriptSerializer - how to deserialize a property with a dash ("-") in it's name?

Trying to deserialize this JSON:

    {
        "result":"success"
        "arguments": {
            "activeTorrentCount":22,
             "cumulative-stats": {
                  "downloadedBytes":1111,
             }
         }
     }

My class:

        private class DeserializationMain
        {
            public string result; //works

            public args arguments; //works, has deserialized activeTorrentCount
            public class args
            {
                public int activeTorrentCount;

                public current cumulative_stats; //doesn't work, equals null
                public class current
                {
                    public long downloadedBytes;
                }
            }
        }

I guess cumulative-stats doesn't get deserialized because it has cumulative_stats variable name in my class, how to deserialize that thing with a dash?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One alternative is to use the DataContractJsonSerializer instead of the JavascriptSerializer.

If you declare your classes like this:

        [DataContract]
        private class DeserializationMain
        {
            [DataMember(Name = "result")]
            public string result; //works
            [DataMember(Name = "arguments")]
            public args arguments; //works, has deserialized activeTorrentCount
            [DataContract]
            public class args
            {
                [DataMember(Name = "activeTorrentCount")]
                public int activeTorrentCount;

                [DataMember(Name = "cumulative-stats")]
                public current cumulative_stats; //doesn't work, equals null
                [DataContract]
                public class current
                {
                    [DataMember(Name = "downloadedBytes")]
                    public long downloadedBytes;
                }
            }
        }

You can deserialize it like this:

string json = "{"result":"success"   ,    "arguments": {  "activeTorrentCount":22,  "cumulative-stats": {   "downloadedBytes":1111      }       }     }";

DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DeserializationMain));
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DeserializationMain result = serializer.ReadObject(ms) as DeserializationMain;

Console.WriteLine("Cumulative-stats.downloadedBytes: "+result.arguments.cumulative_stats.downloadedBytes); 

Will produce: Cumulative-stats.downloadedBytes: 1111


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...