0

我的 Json 格式如下。

{
  "code" : o,
  "message" : "success",
  "book_list":
[
{"name": "C","price":180},
{"name": "C++","price":180},
{"name": "C#","price":180},
]   
}

我是 Windows 手机应用程序开发的全新初学者!我应该如何从 url 获取书籍列表并将其存储在字典之类的东西中??(即作为键值对)对于 windows phone 7?

4

1 回答 1

1

您可以将此 JSON 框架用于 .NET

    using System.Runtime.Serialization;

    [DataContract]
    public class BookShop
    {

        [DataMember(Name = "code")]
        public int Code { get; set; }

        [DataMember(Name = "message")]
        public string Message { get; set; }

        [DataMember(Name = "book_list")]
        public List<Book> Result { get; set; }
    }


    [DataContract]
    public class Book
    {
        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "price")]
        public int Price { get; set; }
    }


 public partial class MainPage : PhoneApplicationPage
{      
    public MainPage()
    {
        InitializeComponent();

        string json = "{\"code\": 0,\"message\": \"success\",\"book_list\": [{\"name\": \"C\",\"price\": 180}, {\"name\": \"C++\",\"price\": 180 }, {\"name\": \"C#\",\"price\": 180}]}";

        var myObjects = JsonConvert.DeserializeObject<BookShop>(json);

        foreach (var item in myObjects.Result)
        {
            Debug.WriteLine("{0} has price {1}", item.Name, item.Price);
        }

    }
}
于 2013-10-14T21:37:05.727 回答