zoukankan      html  css  js  c++  java
  • WP7 Json 解析那些事儿(上)

    Windows Phone7中,我们查阅msdn首先得到的是System.Runtime.Serialization.Json空间下的DataContractJsonSerializer 类,没错,最简单最方便的就是微软自带的这个类.可以通过这个类轻松的将对象序列化为 JSON,或者将 JSON 数据反序列化为对象。但是这个类解析的时候有缺陷,后面的文章再提.

    话不多说,直接切入正题,使用方法:

    1.引入System.ServiceModel.Web.dll

    2.using System.Runtime.Serialization.Json;

    3.代码部分:

        internal static class JsonParse
    {
    public static T Parse<T>(Stream stream)
    {
    return JsonParse._parse<T>(stream);
    }

    //从Json串解析
    private static T _parse<T>(Stream stream)
    {
    return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(stream);
    }

    //将某个序列类合并成Json串,参见:htt p://m s dn.microsoft.com/en-us/library/bb908432(v=VS.96).aspx
    public static string constructJsonString(object jsonObject)
    {
    using (var ms = new MemoryStream())
    {
    new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject);
    byte[] byteArr = ms.ToArray();
    return Encoding.UTF8.GetString(byteArr, 0, byteArr.Length);
    }
    }
    }

    自此,Json的解析工具类制作完毕
    接下来,需要构建一个序列类来测试这个解析工具函数:

                string strJson = @"{'cid':7,'v':'2.0.7.20100715173500','nb':true,'p':true,'l':true,
    'xq':true,'ms':[{'msi':1,'n':'租房','nb':true,'p':true,'l':true,'xq':true},
    {'msi':5,'n':'二手房','nb':true,'p':true,'l':true,'xq':true},
    {'msi':3,'n':'合租房','nb':true,'p':true,'l':true,'xq':true},
    {'msi':10,'n':'日租房\/短期租房','nb':false,'p':true,'l':true,'xq':false},
    {'msi':2,'n':'求租房','nb':false,'p':true,'l':true,'xq':false},
    {'msi':4,'n':'二手房求购','nb':false,'p':true,'l':true,'xq':false},
    {'msi':6,'n':'商铺出租\/求租','nb':false,'p':true,'l':true,'xq':false},
    {'msi':7,'n':'商铺出售\/求购','nb':false,'p':true,'l':true,'xq':false},
    {'msi':8,'n':'写字楼出租\/求租','nb':false,'p':true,'l':true,'xq':false},
    {'msi':9,'n':'写字楼出售\/求购','nb':false,'p':true,'l':true,'xq':false},
    {'msi':11,'n':'厂房\/仓库\/土地','nb':false,'p':false,'l':true,'xq':false}],
    'n':'房产',
    's':3}
    ";

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson));
    SubCategory subCategoryObj = JsonParse.Parse<SubCategory>(ms);

    SubCategory类是根据Json串定义的序列类:

        [DataContract]
    public class SubCategoryItem
    {
    [DataMember(Order = 0)]
    public int msi { get; set; }
    [DataMember(Order = 1)]
    public string n { get; set; }
    [DataMember(Order = 2)]
    public bool nb { get; set; }
    [DataMember(Order = 3)]
    public bool p { get; set; }
    [DataMember(Order = 4)]
    public bool l { get; set; }
    [DataMember(Order = 5)]
    public bool xq { get; set; }
    }
    //详细:htt p://msdn.microsoft.com/zh-cn/library/bb412179.aspx
    [DataContract]
    public class SubCategory
    {
    [DataMember(Order = 0)]
    public int cid { get; set; }
    [DataMember(Order = 1)]
    public string v { get; set; }
    [DataMember(Order = 2)]
    public bool nb { get; set; }
    [DataMember(Order = 3)]
    public bool p { get; set; }
    [DataMember(Order = 4)]
    public bool l { get; set; }
    [DataMember(Order = 5)]
    public bool xq { get; set; }
    [DataMember(Order = 6)]
    public SubCategoryItem[] ms { get;set; }//都是按照名字解析的
    [DataMember(Order = 7)]
    public string n { get; set; }
    [DataMember(Order = 8)]
    public int s { get; set; }

    }

    代码经过本人测试

  • 相关阅读:
    统计与数学必须划出界限
    Maximum likelihood from incomplete data via the EM algorithm (1977)
    Mixtures of Gaussians and the EM algorithm
    http://cs229.stanford.edu/syllabus.html
    Recurrent neural networks are very powerful, because they combine two properties
    multi-layer Recurrent Neural Network (RNN, LSTM, and GRU) for training/sampling from character-level language models
    Turning complete
    hsv hsb rgb lab
    Local Response Normalization 60 million parameters and 500,000 neurons
    Rethinking the Inception Architecture for Computer Vision
  • 原文地址:https://www.cnblogs.com/Peterahan/p/2379226.html
Copyright © 2011-2022 走看看