1

I have an MVC application from which I want to consume an API with the structure highlighted below. However, I got an error

Object reference not set to an instance of an object

on the Web Api controller below

     public IHttpActionResult Post(MyViewModel myViewModel)
     {
         MasterFile ord = myViewModel.MasterFile;
         var ordDetails = myViewModel.LineItems;
         return Ok();
     }

I have structured my models as below:

public class MasterFile
{
    public string payer_prov_name { get; set; }
    public int roaming_amount { get; set; }

    public ICollection<LineItems> LineItems { get; set; }
}

public class LineItems
{
    public string prov_item_name { get; set; }

    [Key]
    public int item_id { get; set; }
}

public class MyViewModel
{
    public MasterFile MasterFile { get; set; }
    public LineItems[] LineItems { get; set; }
}

The structure of the JSON is as below - I have eliminated some fields because they are quite numerous:

(
[0] => Array
    (

        [payer_prov_name] 
        [roaming_amount] 
        [claim_code] 
        [invoices] => Array
            (
                [0] => Array
                    (
                        [line_items] => Array
                            (
                                [0] => Array
                                    (
                                        [prov_item_name] 
                                        [prov_item_code] 
                                        [payer_group_code]  
                                        [item_id] 
                                    )

                                [1] => Array
                                    (
                                        [prov_item_name] 
                                        [prov_item_code] 
                                    )
                            )
                        [payer_benefit_desc]  
                        [payer_benefit_code] 
                        [invoice_id]  
                    )
            )
        [diagnosis] => Array
            (
                [0] => Array
                    (
                        [code] 
                        [coding_standard] 

                    )
            )
        [start_date]  
        [end_date] 
    )
)
) 

Any help on how to structure it would be helpful

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nickson
  • 135
  • 12
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stefan Feb 10 '21 at 10:17
  • `myViewModel` most likely is `null` - you're not receiving the data. Please add a breakpoint. – Stefan Feb 10 '21 at 10:19
  • 1
    That isn't JSON, can you show the *actual* JSON that is being posted? – DavidG Feb 10 '21 at 10:20
  • To be sure about this topics just copy json string from API and use some online json to c# class converters or there is a Visual Studio feature for this on Edit tab https://i.stack.imgur.com/1fuJz.png – Eren Peksen Feb 10 '21 at 10:25

1 Answers1

2

When I was consuming external API, I used JsonConvert.DeserializeObject and JsonConvert.SerializeObject in Newtonsoft.Json.dll. Hovewer, what you posted does not look like json.

RecieveDocument doc = JsonConvert.DeserializeObject<Document>(json);

I have struct RecieveDocument with the properties like in json

public struct RecieveDocument
        {
            public int? Id { get; set; }
            public string DocumentNumber { get; set; }
            public string ExternalDocumentNumber { get; set; }
            public string ExternalId1 { get; set; }
            public IEnumerable<TrackingParameter> TrackingParameters { get; set; }
        }

or I used this

    [JsonProperty("access_token")]
    public string AcessToken { get; set; }
    [JsonProperty("token_type")]
    public string TokenType { get; set; }
    [JsonProperty("expires_in")]
    private int Expires

Serializer can handle those attributes. You can create json from struct/class or create instance from json. Just remember, if you get null, in any json attribute, you must have nullable type.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Vít Bednář
  • 288
  • 2
  • 10