将JSON转换为C类# [英] Convert JSON Into Class C#
问题描述
大家好,
我正在调用一个API,它让我回到JSON响应之下。现在阅读我需要将其转换为类的响应,以便开发人员可以轻松访问它。
Hi Guys,
I am calling an API which is returning me below JSON response. Now to read the response I need to convert it into a class, So that it should be easily accessible for developer.
{
"root": {
"123": {
"license_id": 123,
"name": "Praveen",
"count": 4
},
"456": {
"license_id": 456,
"name": "Ashish",
"count": 0
}
}
}
读取此JSON响应的最佳方法是什么。 JSON响应是返回license_ids的列表,每个许可证都有很少的键,值对,我需要阅读。
我被困在这里因为我不知道我如何将其转换到哪里license_id部分没有名称,而是有值。
我试过 http ://json2csharp.com/ [ ^ ]并返回以下课程,这是我认为不好的。
What would be the best way to read this JSON response. JSON reponse is returning list of license_ids and each license has few key,value pair and that I need to read.
I am stuck here because I don't know how do I convert this where license_id section has no name, instead it has a value.
I tried http://json2csharp.com/[^]and it returned below classes, which is I think not Okay.
public class __invalid_type__123
{
public int license_id { get; set; }
public string name { get; set; }
public int count { get; set; }
}
public class __invalid_type__456
{
public int license_id { get; set; }
public string name { get; set; }
public int count { get; set; }
}
public class Root
{
public __invalid_type__123 __invalid_name__123 { get; set; }
public __invalid_type__456 __invalid_name__456 { get; set; }
}
public class RootObject
{
public Root root { get; set; }
}
请帮帮我们。
Please help guys.
推荐答案
试试这个 - http://json2csharp.com/ [ ^ ]。
Try this - http://json2csharp.com/[^].
首先你必须有一个可以容纳那些数据的类定义...
之后,您应该学习如何使用 Json.NET [ ^ ]库序列化/反序列化对象...
First of all you have to have a class definition that can hold that data...
After that you should learn how to use Json.NET[^] library to serialize/deserialize objects...
这是一个工具:https://jsonclassgenerator.codeplex.com/ [ ^ ]
Here is a tool:https://jsonclassgenerator.codeplex.com/[^]
这篇关于将JSON转换为C类#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!