1

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.

4
  • Your json data is string? Commented Aug 2, 2019 at 22:10
  • You can use json2csharp for converting your json to c# classes. Here is the link. Commented Aug 2, 2019 at 22:40
  • @abhi the jsonutils is better option cuz the json2csharp don't create the root class. Commented Aug 2, 2019 at 22:49
  • in jsonutils i obtain this : add in the request, can you explain me hoow do i use this ? Commented Aug 2, 2019 at 23:34

2 Answers 2

1

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;}
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don't comprend why do you have a viewmodel because i use mvc and not mvvm, Can you please explain how ist is different when you use json in mvc asp.net and in php with mysql.
You will still often create viewmodels with MVC as the various views will require slightly different versions of the models that you are working with. In your example here, you have 3 'models', but you want to combine them and return them for the purposes of this specific view. This is why you would use a 'viewmodel'
it's about matching what I want to do, what I want to do is a select all for each list and a select by name as a primary key as in mysql. it's the first time i've been working with asp.net and i'm having trouble understanding how it all works with json as a bdd.
It sounds like you're moving into a second question. You should post a new question for it.
0

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

2 Comments

if I understand correctly, I would have a string once the json is converted but after how I separate correctly to put it in my 3 listl?
@Dr.LexLab The answer did it. You put your 3 lists in the model class and now you can use them. If this isn't your answer I've been confused, indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.