繁体   English   中英

使用 NewtonSoft 反序列化 JSon 子对象

[英]Deserializing a JSon subobject using NewtonSoft

我是 NewtonSoft 的新手,并试图反序列化一个包含子对象的 object,但我在反序列化时在我的子对象中得到一个 null object...

我的 Json 是:

"{\"id\":\"5e6106600066d227a231ceb8\",\"complete\":null,\"questions\":{\"5e60af61a7be775b0d31ea77\":{\"timeStamp\":\"2020-03-05T15:01:56.000000Z\",\"choices\":[\"dsbb\"]},\"5e60af66a7be775b0d31ea78\":{\"timeStamp\":\"2020-03-05T15:02:02.000000Z\",\"choices\":[\"9999999999\"]},\"5e60af76dd15333d1727ce09\":{\"timeStamp\":\"2020-03-05T15:02:11.000000Z\",\"choices\":[\"lj@test.com\"]},\"5e60afeeb406ed608058d045\":{\"timeStamp\":\"2020-03-05T15:02:15.000000Z\",\"choices\":[0]},\"5e5d282331808f44ce4b0b76\":{\"timeStamp\":\"2020-03-05T15:02:22.000000Z\",\"choices\":[0]},\"5e5cec17ae23a40b0c645614\":{\"timeStamp\":\"2020-03-05T15:02:29.000000Z\",\"choices\":[0]},\"5e5d08d235bf95782b049cb3\":{\"timeStamp\":\"2020-03-05T15:02:34.000000Z\",\"choices\":[2]},\"5e5d0a05a0be6b6533195f17\":{\"timeStamp\":\"2020-03-05T15:02:43.000000Z\",\"choices\":[0]},\"5e5cecdcf3c27f611b3df2fa\":{\"timeStamp\":\"2020-03-05T15:03:01.000000Z\",\"choices\":[\"100\"]},\"5e5cedd7949da059190f2146\":{\"timeStamp\":\"2020-03-05T15:03:10.000000Z\",\"choices\":[1,3,4]},\"5e60e8e899017615e27ad107\":{\"timeStamp\":\"2020-03-05T15:03:15.000000Z\",\"choices\":[0]},\"5e60e95d479b812cb4777b2f\":{\"timeStamp\":\"2020-03-05T15:03:22.000000Z\",\"choices\":[0]},\"5e60e9feff05631d3b0585d8\":{\"timeStamp\":\"2020-03-05T15:03:59.000000Z\",\"choices\":[\"fveg\"]}},\"ip_address\":\"188.165.111.130\",\"created_at\":\"2020-03-05T14:02:08.621000Z\",\"updated_at\":\"2020-03-05T14:04:21.995000Z\"}"

我的 object 定义是:

public class Respondant
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("ip_address")]
    public string IpAddress { get; set; }

    [JsonProperty("created_at")]
    public DateTimeOffset CreatedAt { get; set; }

    [JsonProperty("questions")]
    public Questions Questions { get; set; }

    [JsonProperty("complete")]
    public bool? Complete { get; set; }
}

public partial class Questions
{
    [JsonProperty("questionId")]
    public string QuestionId { get; set; }
}

public partial class QuestionId
{
    [JsonProperty("choices")]
    public string[] Choices { get; set; }
}

我的反序列化代码如下:

//JSon deserialization
JObject dragnsurveyRespondants = JObject.Parse(json);
// get JSON result objects into a list
searchResult = dragnsurveyRespondants.ToObject<Respondant>();
questions = dragnsurveyRespondants["questions"].Children().ToList();

执行此代码会给我一个问题列表,但问题 ID object 应该由问题 ID 和答案的选项卡/列表组成,是 null。

Ant 的想法?

谢谢!

问题字段必须是字典。 因为问题键是动态的。 此外,选择字段必须是 object,因为选择可以是 json 文件中的整数值或字符串值。

就是这样,类是这样的;

public class Respondant
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("ip_address")]
        public string IpAddress { get; set; }

        [JsonProperty("created_at")]
        public DateTimeOffset CreatedAt { get; set; }

        [JsonProperty("questions")]
        public Dictionary<string, Question> Questions { get; set; }

        [JsonProperty("complete")]
        public bool? Complete { get; set; }
    }

    public partial class Question
    {
        [JsonProperty("timeStamp")]
        public DateTime timeStamp { get; set; }

        [JsonProperty("choices")]
        public List<object> choices { get; set; }
    }

使用Newtonsoft.Json库,可以反序列化 object 等;

var respondant = JsonConvert.DeserializeObject<Respondant>(jsonString);

访问问题列表;

var questionList =respondant.Questions

您可以在列表中按键查询。

抱歉,但是您的请求 json 和 class 结构不正确。 这是我可以创造的

public class Respondant
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("ip_address")]
    public string IpAddress { get; set; }

    [JsonProperty("created_at")]
    public DateTimeOffset CreatedAt { get; set; }

    [JsonProperty("questions")]
    public Questions[] Questions { get; set; }

    [JsonProperty("complete")]
    public bool? Complete { get; set; }
}

public partial class Questions
{
    [JsonProperty("questionId")]
    public string QuestionId { get; set; }

    public Question Question { get; set; }
}

public partial class Question
{
    [JsonProperty("choices")]
    public string[] Choices { get; set; }
}

创建 json 并序列化和反序列化 object 的代码

        var question = new Question();

        question.Choices = new string[] { "A", "B", "C" };

        var questions = new List<Questions>();
        questions.Add(new Questions() { QuestionId = "5e6106600066d227a231ceb8", Question = question });

     

        var request = new Respondant()
        {
            Id = "5e6106600066d227a231ceb8",
            Questions = questions.ToArray()
        };
        var jsonObj = Newtonsoft.Json.JsonConvert.SerializeObject(request);


        var response = Newtonsoft.Json.JsonConvert.DeserializeObject<Respondant> (jsonObj);

暂无
暂无

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

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