Reputation: 33
i am new in asp.net developpement and i don't know how to obtain json data with multi array and convert it in 3 model ? example: - array 1 ln, array2 wn and array 3 mangas -model 1 ln , model 2 wn, model 3 manga
{
"LN": [
{
"Name": "Mahouka Koukou no Rettousei",
"Authors": "Satou Tsutomu",
"Artits": "Ishida Kana",
"Year": "2011",
"Volume": 29,
}
],
"WN": [
{
"Name": "Chiyu Mahou no Machigatta Tsukaikata ~Senjou wo Kakeru Kaifuku Youin~",
"Authors": "Kurokata, くろかた",
"Artits": "KeG",
"Year": "2014",
"Chapter": 236,
}
],
"Manga": [
{
"Name": "Tensei Shitara Slime Datta Ken",
"Authors": "Fuse",
"Artits": "Kawakami Taiki",
"Year": "2015",
"Chapter": 60,
}
]
}
public class LN
{
public string Name { get; set; }
public string Authors { get; set; }
public string Artits { get; set; }
public string Year { get; set; }
public int Volume { get; set; }
}
public class WN
{
public string Name { get; set; }
public string Authors { get; set; }
public string Artits { get; set; }
public string Year { get; set; }
public int Chapter { get; set; }
}
public class Manga
{
public string Name { get; set; }
public string Authors { get; set; }
public string Artits { get; set; }
public string Year { get; set; }
public int Chapter { get; set; }
}
I obtain this in jsonutils
public class LN
{
public string Name { get; set; }
public string Authors { get; set; }
public string Artits { get; set; }
public string Year { get; set; }
public int Volume { get; set; }
}
public class WN
{
public string Name { get; set; }
public string Authors { get; set; }
public string Artits { get; set; }
public string Year { get; set; }
public int Chapter { get; set; }
}
public class Manga
{
public string Name { get; set; }
public string Authors { get; set; }
public string Artits { get; set; }
public string Year { get; set; }
public int Chapter { get; set; }
}
public class Example
{
public IList<LN> LN { get; set; }
public IList<WN> WN { get; set; }
public IList<Manga> Manga { get; set; }
}
obtain 3 list: mangas, ln,wn and use this list in the view via the controller. but i don't know how to choose the array in json.
Upvotes: 1
Views: 57
Reputation: 3066
You must deserialize Json Data to the object. For this, you can use the Newtonsoft.Json package. First of put your models in the root class and then convert them with JsonConvert class.
public class Model
{
public IEnumerable<LN> LN { get; set; }
public IEnumerable<WN> WN { get; set; }
public IEnumerable<Manga> Manga { get; set; }
}
Usage
var model = JsonConvert.DeserializeObject<Model>(jsonString);
Upvotes: 0
Reputation: 5028
THat is a pretty rich model to be passign around all the time, but if it is actually how you need to / want to do it, then you would create a view model that is made up of a collection of LN
, a collection of WN
, and a collection of Manga
, and pass around that. Something like:
public class MangasViewModel{
public List<WN> WN{get;set;}
public List<LN> LN {get;set;}
public List<Manga> Manga {get;set;}
}
Upvotes: 1