-1

The json :

{"ipAuthority":1,"data1":[["2020-07-01",1],["2020-07-02",2],["2020-07-03",3],["2020-07-04",4]]}

class root
{
   //??????
}

Then how create the class to format the about json?

TylerH
  • 20,799
  • 66
  • 75
  • 101
A.K
  • 29
  • 2
  • 1
    You can also [use a dynamic object instead of a fixed class](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) if you want to. – CherryDT Aug 03 '20 at 11:42
  • 1
    Welcome to Stack Overflow! Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – Hille Aug 03 '20 at 11:46
  • @CherryDT I want create a class,then I can return the json like above to the webapi user. – A.K Aug 03 '20 at 12:33
  • Wait so your question is the opposite of what you wrote? Not how to convert JSON to a C# class object, but how to write a class that, when serialized, produces given JSON? Maybe you want to edit and clarify your question. – CherryDT Aug 03 '20 at 12:37
  • Hille comment resume the way you should ask a question here. But just to give you an hint about what to learn before asking this question again ... [serialization](https://learn.microsoft.com/it-it/dotnet/standard/serialization/system-text-json-how-to) is the key. – weirdgyn Aug 03 '20 at 13:00

1 Answers1

0

This isn't supported in the framework itself, but many editors have a feature for this included in the box.

Here is an example from Visual studio that will generate your statically typed classes: enter image description here

Once you have the class, you can use the included in .net core to reverse it back to json, under System.Text.Json. You can read more about it at https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to

Eric Johansson
  • 582
  • 2
  • 14
  • I would like to generate the class manually,I have written one. public class Rootobject { public int IPAuthority { get; set; } public List> Data1 { get; set; } = new List>(); } But It's not correct .The wrong json as follows: {"ipAuthority":1,"data1":[["2020-07-01,1"],["2020-07-02,2"],["2020-07-03,3"],["2020-07-04,4"]]} – A.K Aug 04 '20 at 00:35