[英]How to convert this JSON to C# object
I am using an API that returns me the result JSON formatted like this: 我正在使用一个API,该API返回的结果JSON格式如下:
{
"undefined":{
"theresult":{
"back_1s":{
"consumed":1115287.58,
"min_cons":28789,
"max_cons":1086498.58,
"totalobjs":12683,
"totalproces":4298
},
"back_10s":{
"consumed":1115287.58,
"min_cons":28789,
"max_cons":1086498.58,
"totalobjs":12683,
"totalproces":4298
}
}
}
}
The first thing I did was creating a C# object that has 5 properties for each of the JSON's values. 我做的第一件事是创建一个C#对象,该对象具有每个JSON值的5个属性。 Then I de-serialized the JSON string into an array of this new object.
然后,我将JSON字符串反序列化为这个新对象的数组。
However I have no idea what back_1s
is, and what it will represent in C#,not to mention theresult
and the undefined
. 但是我不知道什么
back_1s
是,什么它会在C#中表示,更不用说theresult
和undefined
。
I just see no way for me to de-serialize it without help. 我只是觉得没有帮助,我无法对它进行反序列化。
This is how I de-serialize in C# 这就是我在C#中反序列化的方式
List<NEWOBJECT> gooddata = JsonConvert.DeserializeObject<List<NEWOBJECT>>(jsonstring);
EDIT #1: 编辑#1:
I am getting this error when I deserialize in C# 在C#中反序列化时出现此错误
Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'mvcAndrew.Controllers.NEWOBJECT[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Easiest ist to generate the C# Classes from JSON: http://json2csharp.com/ 从JSON生成C#类的最简单方法: http : //json2csharp.com/
If you deserialize it use the generated RootObject class (or rename the class) that should do the trick. 如果您反序列化它,则应使用生成的RootObject类(或重命名该类)来解决问题。
This will generate two classes for Back1s / Back10s - you can still use only one class (delete the other one) and edit the "Theresult" class corresponding (for example, I renamed Back1s to BackClass and deleted the Back10s class) 这将为Back1s / Back10s生成两个类-您仍然只能使用一个类(删除另一个类)并编辑对应的“ Theresult”类(例如,我将Back1s重命名为BackClass并删除了Back10s类)
public class Theresult
{
public BackClass back_1s { get; set; }
public BackClass back_10s { get; set; }
}
You can also use Newtonsoft.json. 您也可以使用Newtonsoft.json。
var files = JObject.Parse(YourJsonHere);
var recList = files.SelectToken("$..theresult").ToList();
foreach (JObject item in recList.Children())
{
string values = item["consumed"].ToString();
// You can get other values here
}
Need to create a class structure same as your return object may be kind of 需要创建一个与您的返回对象相同的类结构
public class backDetails
{
public double consumed { get; set; }
public double min_cons { get; set; }
public double max_cons { get; set; }
public double totalobjs { get; set; }
public double totalproces { get; set; }
}
public class TheResult
{
public backDetails back_1s { get; set; }
public backDetails back_10s { get; set; }
}
public class MyClass
{
public TheResult theresult { get; set; }
}
Then this will work 这样就可以了
List<MyClass> gooddata = JsonConvert.DeserializeObject<List<MyClass>>(jsonstring);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.