JSON 不反序列化某些值

我有一个基类(用于User类的基类):


public abstract class UserB2C

{

    public List<SignInName> SignInNames { get; set; }

}


public class SignInName

{

    string Type { get; set; }


    string Value { get; set; }

}

还有一些 JSON,其中包括:


\"signInNames\":[{\"type\":\"emailAddress\",\"value\":\"user@yahoo.co.uk\"}],

传递给:


JsonConvert.DeserializeObject<User>(json);

但是创建(反序列化)的对象具有:


    "signInNames": [

        {}

    ],

为什么该字段没有被填充的任何想法?没有产生错误。所有其他(简单)值都可以正常填充。


我试着改变的情况下Type,并Value以JSON字符串匹配,并且还试图明确创建List<SignInName>时创建的对象,但无济于事。


噜噜哒
浏览 404回答 1
1回答

UYOU

SignInName类的属性应该声明为public,以便用值反序列化。public class SignInName{&nbsp; &nbsp; public string Type { get; set; }&nbsp; &nbsp; public string Value { get; set; }}更新这是一个最小、完整且可验证的示例:using Newtonsoft.Json;using System.Collections.Generic;class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var json =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "{\"signInNames\":[{\"type\":\"emailAddress\",\"value\":\"user@example.com\"}]}";&nbsp; &nbsp; &nbsp; &nbsp; var user = JsonConvert.DeserializeObject<User>(json);&nbsp; &nbsp; &nbsp; &nbsp; System.Console.WriteLine(JsonConvert.SerializeObject(user));&nbsp;&nbsp; &nbsp; }}public abstract class UserB2C{&nbsp; &nbsp; public List<SignInName> SignInNames { get; set; }}public class User : UserB2C {&nbsp; }public class SignInName{&nbsp; &nbsp; string Type { get; set; }&nbsp; &nbsp; string Value { get; set; }}输出是 {"SignInNames":[{}]}如果我们创建SignInName类属性public,输出将是:{"SignInNames":[{"Type":"emailAddress","Value":"user@example.com"}]}
打开App,查看更多内容
随时随地看视频慕课网APP