简体   繁体   English

如何将这个JSON到C#对象转换

[英]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#中表示,更不用说theresultundefined

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);

如何转换列表<object>至 Json | C#<div id="text_translate"><p> 我有一个由对象组成的列表,每个 object 有 5 个数据。 我需要将该列表转换为 json,但使用序列化它会填满空的 json。</p><p> 有谁知道我可能做错了什么?</p><pre> foreach (DataRow dtRow in dtAlarmas.Rows) { String Name = dtRow["Name"].ToString(); String ID = dtRow["ID"].ToString(); String AlarmText = dtRow["AlarmText"].ToString(); String AlarmTimeNoNula = dtRow["AlarmTimeNoNula"].ToString(); lstAlarmasNoTratadas.Add(new Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel(dtRow["Name"].ToString(), Convert.ToInt32(dtRow["ID"]), dtRow["Class"].ToString(), dtRow["AlarmText"].ToString(), dtRow["AlarmTimeNoNula"].ToString())); } string sParams = JsonConvert.SerializeObject(lstAlarmasNoTratadas);</pre><p> 转换后的 sParams 值 = "[{}]"</p><p> Class Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel</p><pre> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ondoan.DatosAux.Alarmas { public class AlarmaNoTratadaModel { private string Name; private int ID; private string Class; private string AlarmText; private string AlarmaTimeNoNula; public AlarmaNoTratadaModel(string Name, int ID, string Class, string AlarmText, string AlarmaTimeNoNula) { // TODO: Complete member initialization this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula; } public class AlarmaNoTratadasModel { public AlarmaNoTratadasModel() { } public AlarmaNoTratadasModel(String Name, Nullable&lt;System.Int32&gt; ID, String Class, String AlarmText, String AlarmaTimeNoNula) { this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula.ToString(); } public System.String Name { get; set; } public Nullable&lt;System.Int32&gt; ID { get; set; } public System.String Class { get; set; } public System.String AlarmText { get; set; } public System.String AlarmaTimeNoNula { get; set; } } } }</pre></div></object> - How to Convert List<Object> to Json | C#

暂无
暂无

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

相关问题 如何将C#对象转换为JSON对象 - How to convert c# object to json object 如何在C#中将json字符串转换为对象 - how to convert json string to object in C# 如何将此json转换为C#对象 - How to convert this json into C# object 如何在C#中将Json对象转换为数组 - How to Convert Json Object to Array in C# 如何转换列表<object>至 Json | C#<div id="text_translate"><p> 我有一个由对象组成的列表,每个 object 有 5 个数据。 我需要将该列表转换为 json,但使用序列化它会填满空的 json。</p><p> 有谁知道我可能做错了什么?</p><pre> foreach (DataRow dtRow in dtAlarmas.Rows) { String Name = dtRow["Name"].ToString(); String ID = dtRow["ID"].ToString(); String AlarmText = dtRow["AlarmText"].ToString(); String AlarmTimeNoNula = dtRow["AlarmTimeNoNula"].ToString(); lstAlarmasNoTratadas.Add(new Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel(dtRow["Name"].ToString(), Convert.ToInt32(dtRow["ID"]), dtRow["Class"].ToString(), dtRow["AlarmText"].ToString(), dtRow["AlarmTimeNoNula"].ToString())); } string sParams = JsonConvert.SerializeObject(lstAlarmasNoTratadas);</pre><p> 转换后的 sParams 值 = "[{}]"</p><p> Class Ondoan.DatosAux.Alarmas.AlarmaNoTratadaModel</p><pre> using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ondoan.DatosAux.Alarmas { public class AlarmaNoTratadaModel { private string Name; private int ID; private string Class; private string AlarmText; private string AlarmaTimeNoNula; public AlarmaNoTratadaModel(string Name, int ID, string Class, string AlarmText, string AlarmaTimeNoNula) { // TODO: Complete member initialization this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula; } public class AlarmaNoTratadasModel { public AlarmaNoTratadasModel() { } public AlarmaNoTratadasModel(String Name, Nullable&lt;System.Int32&gt; ID, String Class, String AlarmText, String AlarmaTimeNoNula) { this.Name = Name; this.ID = ID; this.Class = Class; this.AlarmText = AlarmText; this.AlarmaTimeNoNula = AlarmaTimeNoNula.ToString(); } public System.String Name { get; set; } public Nullable&lt;System.Int32&gt; ID { get; set; } public System.String Class { get; set; } public System.String AlarmText { get; set; } public System.String AlarmaTimeNoNula { get; set; } } } }</pre></div></object> - How to Convert List<Object> to Json | C# 如何使用C#将JSON转换为对象 - How to convert JSON to object with C# 如何在C#中将json数据转换为对象? - How to convert json data to object in C#? 如何将 Json 转换为 C# object? - How to convert Json into C# object? 如何在C#中将json对象转换为字符串? - How to convert json object to string in C#? 如何将动态 JSON 转换为 C# object - How to convert dynamic JSON to C# object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM