Home > Back-end >  c# how to create sample array form json data
c# how to create sample array form json data

Time:08-26

my json

[{"m":0},{"m":1},{"m":2},{"m":3},{"m":5}]

I need create array

[0,1,2,3,5]

this is my code but don't work

public class vclass
    {
        public int m { get; set; }
    
    }
public class iResponse
    {   
        public List<vclass> m { get; set; }
    }

iResponse result = JsonConvert.DeserializeObject<iResponse>(res);

CodePudding user response:

You just need this:

var res = @"[{""m"":0},{""m"":1},{""m"":2},{""m"":3},{""m"":5}]";
vclass[] result = JsonConvert.DeserializeObject<vclass[]>(res);

That gives your array.

CodePudding user response:

Create this 2 classes.

public class JsonList {

public List<M> Ms { get; set; }

}

public class M {

public int m { get; set; }

}

Then var result = JsonConvert.DeserializeObject("your json response");

After that:

var myArray = result.Ms.ToArray();

  • Related