【摘要】JSON.NET

JSON.NET
(http://james.newtonking.com/default.aspx)
http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object

A good way to use JSON in C# is with JSON.NET

Quick Starts & API Documentation from JSON.NET - Official site help you work with it.

An example of how to use it:

public class User {
        public User(string json) {
            JObject jObject = JObject.Parse(json);
            JToken jUser = jObject["user"];
            name = (string) jUser["name"];
            teamname = (string) jUser["teamname"];
            email = (string) jUser["email"];
            players = jUser["players"].ToArray();
        }

        public string name { get; set; }
        public string teamname { get; set; }
        public string email { get; set; }
        public Array players { get; set; }
    }
    // Use
    private void Run() {
        string json = @"{""user"":{""name"":""asdf"",
             ""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
        User user = new User(json);
        Console.WriteLine("Name : " + user.name);
        Console.WriteLine("Teamname : " + user.teamname);
        Console.WriteLine("Email : " + user.email);
        Console.WriteLine("Players:");
        foreach (var player in user.players)
            Console.WriteLine(player);
     }
posted @ 2012-08-13 23:07  一 缕 阳 光  阅读(197)  评论(0编辑  收藏  举报