简体   繁体   English

使用 C# .NET 内核反序列化 json

[英]Deserialize json with C# .NET Core

Im trying to deserialize data that Ive got over POST in JSON format but having some problem.我试图以 JSON 格式反序列化我通过 POST 获得的数据,但有一些问题。

The error message is:错误信息是:

SerializationException: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''.序列化异常:期望 state '元素'..遇到名称为'',命名空间''的'文本'。 System.Runtime.Serialization.XmlObjectSerializerReadContext.HandleMemberNotFound(XmlReaderDelegator xmlReader, ExtensionDataObject extensionData, int memberIndex) System.Runtime.Serialization.XmlObjectSerializerReadContext.HandleMemberNotFound(XmlReaderDelegator xmlReader,ExtensionDataObject extensionData,int memberIndex)

Controller where the serialization is happening: Controller 发生序列化:

    public String RequestToken(string userData)
    {
            Contract.Ensures(Contract.Result<string>() != null);
            UserModel deserializedUser = new UserModel();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(userData));
            ms.Position = 0;
            DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());
            deserializedUser = ser.ReadObject(ms) as UserModel;
    }

UserModel that is used as a contract:用作合约的 UserModel:

using System;
using System.Runtime.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace WishareIntegrationApi.Entities
{
    [DataContract]
    public class UserModel
    {
        [BsonId]
        [BsonRepresentation(BsonType.String)]
        [DataMember]
        public ObjectId _id { get; set; }
        [DataMember]
        public string displayName { get; set; }
        [DataMember]
        public string photoURL { get; set; }
        [DataMember]
        public string email { get; set; }
        [DataMember]
        public int registeredAt { get; set; }
    }
}

And an example JSON i'm sending over post:还有一个示例 JSON 我通过邮寄发送:

{"_id":"8kmXH1fzSrVS8PqNLMwyhRH4hBw1","displayName":"Michal Takáč","photoURL":"https://lh3.googleusercontent.com/-xa5oE48RffQ/AAAAAAAAAAI/AAAAAAAACDE/OLrtV5-VIvw/photo.jpg","email":"email.takac@gmail.com"}

Switch to JSON.Net. 切换到JSON.Net。

JSON serialization APIs are not part of .Net core and I don't expect them to port that over. JSON序列化API不是.Net核心的一部分,我不希望它们移植它。 If you used classes from namespaces like System.Web.Script.Serialization switch to other serialization, in particular Microsfot frameworks based on .Net core use JSON.Net serializers. 如果您使用来自System.Web.Script.Serialization等名称空间的类切换到其他序列化,特别是基于.Net核心的Microsfot框架使用JSON.Net序列化程序。

As mentioned by many users in comments, I've switched from old way of doing serialization/deserialization using contracts to JSON.NET 正如许多用户在评论中提到的那样,我已经从使用契约进行序列化/反序列化的旧方法切换到JSON.NET

Here is the correct solution for the controller 这是控制器的正确解决方案

public async Task<String> RequestToken(string userData)
{
     var user = JsonConvert.DeserializeObject<UserModel>(userData);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM