如何定义类来反序列化json数组 [英] How to define class to deserialize the json array
本文介绍了如何定义类来反序列化json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
How to define a class to deserialize the below Json Array
{"Errors":[{"Code":123,"Message":"Your account is Linked"}]}
请帮我这个
我是什么尝试过:
我使用下面的类进行反序列化但获得异常。
公共类ErrorDescription
{
公共字符串消息{get; set;}
public int Code {get; set;}
}
公共类错误
{
public List< errordescription> ;;
}
Please help me on this
What I have tried:
I use the below class to Deserialize but getting the exception.
Public class ErrorDescription
{
public string Message {get;set;}
public int Code{get;set;}
}
public class Errors
{
public List<errordescription>;
}
推荐答案
使用此网站从原始JSON生成类: JSON Utils:从JSON生成C#,VB.Net,SQL表,Java和PHP [ ^ ]
更多工具和信息可以在这里找到:在C#中使用JSON& VB [ ^ ]
Use this website for generating classes from raw JSON: JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]
More tools and information can be found here: Working with JSON in C# & VB[^]
您好,请通过以下链接获取帮助。
[ ^ ]
Hi, please go through the below link for your help.
[^]
除了解决方案1和2之外,请尝试以下使用 JavaScriptSerializer类 [ ^ ]用于反序列化,
请使用System.Collections.Generic添加引用System.Web.Extensions.dll
In addition to Solution 1 and 2, try the below code which uses JavaScriptSerializer Class [^] for Deserialization,
Please add the referenceSystem.Web.Extensions.dll
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace CPTemp
{
public class Class1
{
static void Main(string[] args)
{
string input = "{\"Errors\":[{\"Code\":123,\"Message\":\"Your account is Linked\"}]}";
JavaScriptSerializer js = new JavaScriptSerializer();
ErrorInfo obj = js.Deserialize<ErrorInfo>(input);
foreach (ErrorDescription item in obj.Errors)
{
string message = item.Message;
int code = item.Code;
}
}
}
public class ErrorDescription
{
public string Message { get; set; }
public int Code { get; set; }
}
public class ErrorInfo
{
public List<ErrorDescription> Errors { get; set; }
}
}
这篇关于如何定义类来反序列化json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文