json - Using a Variable as List Type in C# -


I am currently using a list to handle a JSON string which works fine for an example of this As I can see, what do I want to do to handle these methods that the conversion is completely normal so I can use them for multiple JSON strings.

It stands as a piece of my current code.

  public class GetPerson {public string fooName {get; Set; } Public string fooAddress {get; Set; } Public string fooPosition {get; Set; }} Public class GetPosition {public string fooTitle {get; Set; } Public string fooDepartment {get; Set; } Public string fooSalary {get; Set; }} Private static list & gt; GetPerson & gt; Convertlists (string jsonstring) {list & lt; ListJson = New list & lt; JsonObject & gt; (); ListJson = (List & gt; GetPerson & gt;) JsonConvert.DeserializeObject & lt; List & gt; GetPerson & gt; & Gt; (JsonString); Return list Jason; }    

This is just a quick sample but list & lt; GetPerson & gt; If I need to be normal, then it can be reused, because this gatepad stands, obviously it will not work with it, because by changing it through my code Want to change

Is there a way to specify a variable in a form? I saw another question about this, but it did not come in detail. Or is there another way that it can be achieved?

Thanks in advance.

To help you reuse the ConvertToList function for different types Can use generics for

  private static list & lt; T & gt; ConvertToList & LT; T & gt; (String jsonString) {return (list & lt; t & gt;) JsonConverty.DeserializeObject & LT; From the list & lt; T & gt; & Gt; (JsonString ();}   

You can now call it using both GetPerson and GetPosition as the default type Listopfile = ConvertToList & gt; GetPosition & gt; (positionJson); ListProople = ConvertToList. P> You can read more about generics at

Besides, if all that is what you want to do is [de] to order JSON, you might want. To consider the library, or, all of which You are trying to do the tasks.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -