0

I am trying to name my array of objects, but it lists only the result of the array being created:

public class Listagem {
    public List<Captura> captura { get; set; }
}

my class

public class Captura {
    public int Codigo { get; set; }
    public string FotoURL { get; set; } 
}

my controller

public IactionResult(Listagem models) {

var resultJson = JsonConvert.SerializeObject(models.captura);

...

Current results:

[
   {
     "Cod":11111,
     "photo":xxxx,

Expected outcome:

{
   "captura":[
   {
     "Cod":11111,
     "photo":xxxx,
2
  • What is models? Is it of type Listagem or something else? If of type Listagem did you try JsonConvert.SerializeObject(captura);? Commented Sep 29, 2019 at 19:11
  • models is the name of the object I instantiated from my Listagem class, to access its attributes and methods. Commented Sep 29, 2019 at 19:16

1 Answer 1

2

You have

    var resultJson = JsonConvert.SerializeObject(models.captura);

but you want

    var resultJson = JsonConvert.SerializeObject(models);

Type names don't matter. Local variable names don't matter. The contents of the expression used to get the argument to SerializeObject doesn't matter. Only member names matter.

Most likely, you wrote the above because you have other stuff in models you don't want serialized. In this case, you should do

public class CapturaModel {
    public IList<Captura> captura;
};


    //...
    var resultJson = JsonConvert.SerializeObject(new CapturaModel { captura = models.captura; });

This code also shows why the contents of the expression used get the argument to SerializeObject doesn't matter.

Sign up to request clarification or add additional context in comments.

Comments

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.