首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图反序列化时的System.Runtime.Serialization.SerializationException

问试图反序列化时的System.Runtime.Serialization.SerializationException
EN

Stack Overflow用户
提问于 2014-08-14 10:38:20
回答 1查看 4.8K关注 0票数 4

我有下面的json字符串,我正在尝试反序列化

代码语言:javascript
复制
[{\"sha\":\"29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"commit\":{\"author\":{\"name\":\"Christophe Coevoet\",\"email\":\"stof@notk.org\",\"date\":\"2013-03-06T08:53:13Z\"},\"committer\":{\"name\":\"Christophe Coevoet\",\"email\":\"stof@notk.org\",\"date\":\"2013-03-06T08:53:13Z\"},\"message\":\"Merge pull request #1071 from BrandonLWhite/master\n\nThis resolves Issue #1055\",\"tree\":{\"sha\":\"277ae283e984236883a18bf7e2c703abc17ce48a\",\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/git/trees/277ae283e984236883a18bf7e2c703abc17ce48a\"},\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/git/commits/29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"comment_count\":0},\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"html_url\":\"https://github.com/ComparetheMarket/chosen/commit/29a5ac11a67451d1b8bb6e525857cf35587334a2\",\"comments_url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/29a5ac11a67451d1b8bb6e525857cf35587334a2/comments\",\"author\":{\"login\":\"stof\",\"id\":439401,\"avatar_url\":\"https://avatars.githubusercontent.com/u/439401?v=2\",\"gravatar_id\":\"7894bbdf1c05b18a1444ad8c76c9d583\",\"url\":\"https://api.github.com/users/stof\",\"html_url\":\"https://github.com/stof\",\"followers_url\":\"https://api.github.com/users/stof/followers\",\"following_url\":\"https://api.github.com/users/stof/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/stof/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/stof/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/stof/subscriptions\",\"organizations_url\":\"https://api.github.com/users/stof/orgs\",\"repos_url\":\"https://api.github.com/users/stof/repos\",\"events_url\":\"https://api.github.com/users/stof/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/stof/received_events\",\"type\":\"User\",\"site_admin\":false},\"committer\":{\"login\":\"stof\",\"id\":439401,\"avatar_url\":\"https://avatars.githubusercontent.com/u/439401?v=2\",\"gravatar_id\":\"7894bbdf1c05b18a1444ad8c76c9d583\",\"url\":\"https://api.github.com/users/stof\",\"html_url\":\"https://github.com/stof\",\"followers_url\":\"https://api.github.com/users/stof/followers\",\"following_url\":\"https://api.github.com/users/stof/following{/other_user}\",\"gists_url\":\"https://api.github.com/users/stof/gists{/gist_id}\",\"starred_url\":\"https://api.github.com/users/stof/starred{/owner}{/repo}\",\"subscriptions_url\":\"https://api.github.com/users/stof/subscriptions\",\"organizations_url\":\"https://api.github.com/users/stof/orgs\",\"repos_url\":\"https://api.github.com/users/stof/repos\",\"events_url\":\"https://api.github.com/users/stof/events{/privacy}\",\"received_events_url\":\"https://api.github.com/users/stof/received_events\",\"type\":\"User\",\"site_admin\":false},\"parents\":[{\"sha\":\"70c46cfac7cb281af61d39f352d333eda9f1a84b\",\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/70c46cfac7cb281af61d39f352d333eda9f1a84b\",\"html_url\":\"https://github.com/ComparetheMarket/chosen/commit/70c46cfac7cb281af61d39f352d333eda9f1a84b\"},{\"sha\":\"b1f85c040a4aca3cd3c1ec278264952eac7ea0f0\",\"url\":\"https://api.github.com/repos/ComparetheMarket/chosen/commits/b1f85c040a4aca3cd3c1ec278264952eac7ea0f0\",\"html_url\":\"https://github.com/ComparetheMarket/chosen/commit/b1f85c040a4aca3cd3c1ec278264952eac7ea0f0\"}]}]

使用这种反序列化方法

代码语言:javascript
复制
public List<RepositoryCommitData> DeserializeGithubCommitUrlJsonResponseAndTripUneededData(string json)
    {
        var serializer = new DataContractJsonSerializer(typeof (List<RepositoryCommitData>));
        var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
        var desirializedData = (List<RepositoryCommitData>) serializer.ReadObject(stream);

        return desirializedData;
    }

进入以下类

代码语言:javascript
复制
[DataContract]
public class RepositoryCommitData
{
    [DataMember] public Committer commit;
}
[DataContract]
public class Committer
{
    [DataMember] public string name;
    [DataMember] public string date;
}

但我总是收到

System.Runtime.Serialization.SerializationException :反序列化System.Collections.Generic.List`1[GitReport.Deserialization.RepositoryCommitData,GitReport、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null类型的对象时出错。遇到无效字符“”。-> System.FormatException :遇到无效字符‘’。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-14 11:01:51

输入字符串中有两个换行符(在输入文本中查找\n )。如果转义(或删除)这些错误,原始错误将消失。Here是在反序列化之前应该转义的控制字符列表。

在原来的JSON字符串中,我收到了另一个错误,但是当您更新它时,我所要做的就是用\\n转义这个\\n。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25306025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档