简体   繁体   English

如何将Json反序列化为对象?

[英]How to deserialize Json to an object?

I want to convert json to a specific object. 我想将json转换为特定对象。

String : "{\\r\\n \\"Status\\": \\"PLANNED\\"\\r\\n}"

I tried Newtonsoft Json namespace but it is returning a null value. 我尝试了Newtonsoft Json命名空间,但它返回的是空值。

var Json= Newtonsoft.Json.JsonConvert.DeserializeObject<Model Class>(String )

I want the result in Json format so that I can extract the value from Json as "PLANNED" but I am getting a null. 我想要Json格式的结果,以便可以将Json的值提取为“ PLANNED”,但我得到的是空值。

PS :The model class contains two properties , Name (type of string), Value(type of Object) PS :模型类包含两个属性,名称(字符串类型),值(对象类型)

var s = "{\r\n  \"Status\": \"PLANNED\"\r\n}";
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<StatusModel>(s);

The model you have defined is incorrect. 您定义的模型不正确。 Your model should be like this: 您的模型应如下所示:

public class StatusModel
{
    public string Status { get; set; }
}

Now value will be extracted to this model and you can access the value like: 现在,将值提取到该模型,您可以像这样访问该值:

var value = obj.Status; //"PLANNED"

JSON Definition JSON定义

JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON(JavaScript对象表示法)是一种轻量级的数据交换格式。 It is easy for humans to read and write. 人类易于阅读和书写。 It is easy for machines to parse and generate. 机器很容易解析和生成。 It is based on a subset of the JavaScript Programming Language. 它基于JavaScript编程语言的子集。

[Source] https://www.json.org/ [来源] https://www.json.org/

JSON Newtonsoft JSON牛顿软件

Json.NET is a popular high-performance JSON framework for .NET . Json.NET是用于.NET的流行的高性能JSON框架。

[Source] https://www.newtonsoft.com/json [来源] https://www.newtonsoft.com/json

Problem : 问题:

Your are trying to deserialize a json to an object and it's returning a null. 您正在尝试将json反序列化为对象,并且返回null。 In our context a Deserialization is process which transform a json to an object . 在我们的上下文中, 反序列化是将json转换为对象的过程。

var Result= Newtonsoft.Json.JsonConvert.DeserializeObject<Model Class>(String);

The reason why you have a Null as result is beacause you are deserializing a json to Model, knowing that you Json does not correspond to the Model , this is why the Json need to correspond to the Model so that it can store the information of the Json . 结果为Null的原因是,因为您知道Json不对应于Model ,所以要将json反序列化为Model ,这就是Json需要对应于Model以便可以存储模型信息的原因。 杰森

Your Model : 您的模特:

The model may contain some property that are not in the json and vice versa 该模型可能包含一些不在json中的属性,反之亦然

public class StatusModel
{
   public string Status { get; set; }
}

Best Regards . 最好的祝福 。

You can do it like this (using Newtonsoft Framework) 您可以这样做(使用Newtonsoft Framework)

using System;
using Newtonsoft.Json;
{
    public class JsonHandler : IJsonHandler
    {
        public IJsonModel ReadJson(IJsonModel model, StreamReader reader)
        {
            try
            {
                string jsonFromFile;
                using (reader))
                {
                    jsonFromFile = reader.ReadToEnd();
                }

                status = JsonConvert.DeserializeObject<model>(jsonFromFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return status;
        }
    }
}

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

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