0
I’m trying to call a web API that returns me a json with multiple values as specified below
json received
[
{
"total_documents": 2,
"total_in_this_page": 2,
"current_page": 1,
"total_pages": 1
},
{
"nameDoc": "2.pdf",
"type": "application/pdf",
"size": "6053"
},
{
"nameDoc": "1.pdf",
"type": "application/pdf",
"size": "6053"
}
]
Class documents info
public class DocumentsInfo
{
[JsonProperty("total_documents")]
public int TotalDocuments { get; set; }
[JsonProperty("total_in_this_page")]
public int TotalInThisPage { get; set; }
[JsonProperty("current_page")]
public int CurrentPage { get; set; }
[JsonProperty("total_pages")]
public int TotalPages { get; set; }
}
Document class
public class Document
{
public string NameDoc { get; set; }
public string Type { get; set; }
public string Size { get; set; }
}
root document class
public class Documents
{
public DocumentsInfo DocumentInfo { get; set; }
public IList<Document> Documentos { get; set; }
}
I’m doing it this way and it’s not working
var documents = JsonConvert.DeserializeObject<IList<Documents>>(response.Content);
does not return error, but does not load objects
[
{
documentInfo: null,
documentos: null
},
{
documentInfo: null,
documentos: null
},
{
documentInfo: null,
documentos: null
}
]
Should return an object documentInfo and two documents
public IList<Document> Documentos { get; set; }
this ta wrong, looks at json, does not return a list of Documents, this json does not seem right, pq it repeats the documents without being in an array, the right would be[ { "nameDoc": "2.pdf", .... }, { "nameDoc": "1.pdf", ... } ]
, note that to be an array/list you must have brackets. Confirm this result, it does not make sense pq, an hour can come 1 or 2 or 10 documents, this has to be in an array with[ ]
, or the "header" that has "total_documents" should be outside the[ ]
in json, so for example:{ "total_documents" } [ { "nameDoc" } ]
– Ricardo Pontual
Are you sure that the structure of JSON is this? Although it is syntactically valid, this JSON is very strange... The first object is completely different from the others.
– Jéf Bueno
The json format is correct, it is received in the format that is mentioned above
– Luiz Cherpers
If json is the same, the class structure is wrong, it will never deserialize an object for an Ilist, unless you write your own deserialize or make the conversion in your hand. Again, this json although valid doesn’t make sense, look at the documentation of this API to see if you have more information about the contract
– Ricardo Pontual