在C#中将JSON反序列化为类 [英] Deserialize JSON to class in C#

查看:96
本文介绍了在C#中将JSON反序列化为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的字符串(json)只包含以下部分,我可以在newtonsoft的库的帮助下反序列化它。



{代码:MXXXXX ,状态:失败}



反序列化代码:



公共类帐户

{

公共字符串代码{get;组; }

public string Status {get;组; }

}

账户账户= JsonConvert.DeserializeObject(json);



Console.WriteLine(账户。代码);



但如果我的字符串是这样的:



{'data':'{ 代码:MXXXXX,状态:失败}}}



我无法反序列化。这里的班级只有一个属性是数据......我怎么能这样做?



我尝试了什么:



反序列化代码:

公共类账户
{
公共字符串代码{get;组; }
public string Status {get;组; }
}
帐户帐户= JsonConvert.DeserializeObject(json);

Console.WriteLine(account.Code);

解决方案

试试这个



 static void Main(string [] args)
{
string json ={'data':{\Code \ :\MXXXXX \,\状态\:\failed\}}; //照顾json,它应该是一个有效的json格式
MyAccount account = JsonConvert.DeserializeObject< MyAccount>(json);
Console.WriteLine(account.data.Code);

}





公共类MyAccount 
{
公共账户数据{get;组; }
}
公共类帐户
{
公共字符串代码{get;组; }
public string Status {get;组; }
}


检查一下:反序列化JSON在C#中排名 [ ^ ]

使用System; 
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;

名称空间Rextester
{
公共类程序
{
public static void Main(string [] args)
{
string json ={\data \:{\Code \:\MXXXXX \,\Status \:\failed\}};

var jsonObj = JsonConvert.DeserializeObject< JObject>(json).First.First;

Console.WriteLine(jsonObj [Code]);
}

公共类账户
{
公共字符串代码
{
get;
设定;
}

公共字符串状态
{
get;
设定;
}
}
}
}

参考: JObject类 [ ^ ]


If my string(json) contain only following part, I am able to deserialize it with the help of newtonsoft's library.

{"Code": "MXXXXX", "Status": "failed"}

Code to deserialize:

public class Account
{
public string Code{ get; set; }
public string Status{ get; set; }
}
Account account = JsonConvert.DeserializeObject(json);

Console.WriteLine(account.Code);

But if my string is like this:

{'data': ' {"Code": "MXXXXX", "Status": "failed"}'}

I am unable to deserialize. Here the class has only one property which is data... how can I do that?

What I have tried:

Code to deserialize:

public class Account
{
    public string Code{ get; set; }
    public string Status{ get; set; }
}
Account account = JsonConvert.DeserializeObject(json);

Console.WriteLine(account.Code);

解决方案

try this

static void Main(string[] args)
       {
           string json = "{'data':  {\"Code\": \"MXXXXX\", \"Status\": \"failed\"}}"; // take care of the json, it should be a valid json format
           MyAccount account = JsonConvert.DeserializeObject<MyAccount>(json);
           Console.WriteLine(account.data.Code);

       }



public class MyAccount
   {
       public Account data { get; set; }
   }
   public class Account
   {
       public string Code { get; set; }
       public string Status { get; set; }
   }


Check this out: Deserialize JSON to class in C#[^]

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string json = "{\"data\":{\"Code\": \"MXXXXX\", \"Status\": \"failed\"}}";

            var jsonObj = JsonConvert.DeserializeObject<JObject>(json).First.First;
		
            Console.WriteLine(jsonObj["Code"]);
        }

        public class Account
        {
            public string Code
            {
                get;
                set;
            }

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

Reference: JObject Class[^]


这篇关于在C#中将JSON反序列化为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆