【问题标题】:json serialization c# windows phone and appc# windows phone and app json序列化
【发布时间】:2015-11-01 16:07:05
【问题描述】:

我是为 WindowsPhone 和 Windows 8.1 编写应用程序的新手。

我有以下问题:我无法通过 Windowsphone 中的 Datacontract 序列化 json 字符串,但在 Windows 8.1 中它可以正常工作。

我的错误信息:(SerializationException):

反序列化类型对象时出错 System.Collections.Generic.List`1[[htwapp.SpeicherStrukturen.Lektures, htwapp.WindowsPhone,版本=1.0.0.0,文化=中性, PublicKeyToken=null]]。输入字符串的格式不正确。

我的目标类:

[DataContract]
    public class Lektures
    {
        [DataMember]
        public string Status { get; set; }
        [DataMember]
        public int Semester { get; set; }
        [DataMember]
        public string PrDatum { get; set; }
        [DataMember]
        public string VoDatum { get; set; }
        [DataMember]
        public string PrNr { get; set; }
        [DataMember]
        public string EctsGrade { get; set; }
        [DataMember]
        public string Vermerk { get; set; }
        [DataMember]
        public string PrNote { get; set; }
        [DataMember]
        public int EctsCredits { get; set; }
        [DataMember]
        public string PrTxt { get; set; }
        [DataMember]
        public string Versuch { get; set; }
        [DataMember]
        public string PrForm { get; set; }

 ... }

我将字符串序列化为目标类的方法

private static List<Lektures> json2cource(String Jason)
        {
            List<Lektures> courcelist = new List<Lektures>() ;
            try {
                MemoryStream stream1 = new MemoryStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Lektures>));
                courcelist = (List<Lektures>)ser.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(Jason)));
                return courcelist;
            }catch (SerializationException e)
            {
                Debug.WriteLine(e.Message);
                return courcelist;
            }
        }

现在是一个示例 Json 字符串:

[{
    "Status":"AB",
    "Semester":"20122",
    "PrDatum":"14.02.2013",
    "VoDatum":"",
    "PrNr":"1111",
    "EctsGrade":"",
    "Vermerk":"",
    "PrNote":"100",
    "EctsCredits":"5.0",
    "PrTxt":"TEXT",
    "Versuch":"1",
    "PrForm":"S"}, ...]

获取字符串的方法是一样的(post requests)。 windows 版本中的字符串与 windowsphone 版本中的字符串相同。

如果有人能帮助我,我会很高兴:)

问候

【问题讨论】:

  • 有什么理由不想使用其他json序列化技术比如json.net
  • Visual Studio 2015 告诉我 json.net 函数在 windowsphone 上不起作用。我想为这两个平台创建一个解决方案。
  • @kalenderdose,它在哪里说的? JSON.NET 支持所有 .NET 平台。它是迄今为止最可靠的 .NET JSON 库。
  • 好的,我发现了错误。我是通过 nuget 安装的,但好像他没有为我的 windowsphone 项目安装它...

标签: c# windows-phone-8 windows-phone windows-applications


【解决方案1】:

我仍然会建议 json.net 这样做。 它也适用于所有 windows phone 版本,我也在我自己的项目中使用它。

您可以简单地序列化您自己的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多