-2

I'm trying to get values back from this piece of code. Everything looks okay to me and the deserialization doesn't give any errors so I'm assuming it all went okay there.

If i do a count on the var I get a value back.

Here is my JSON:

{
                "Activity": [
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "LA",
                                "StateProvinceCode": "CA",
                                "CountryCode": "US"
                            }
                        },
                        "Status": {
                            "Type": "I",
                            "Description": "Departure Scan",
                            "Code": "DP"
                        },
                        "Date": "20171201",
                        "Time": "033400"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "LA",
                                "StateProvinceCode": "CA",
                                "CountryCode": "US"
                            }
                        },
                        "Status": {
                            "Type": "I",
                            "Description": "Origin Scan",
                            "Code": "OR"
                        },
                        "Date": "20171130",
                        "Time": "185600"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "CountryCode": "US"
                            }
                        },
                        "Status": {
                            "Type": "M",
                            "Description": "Order Processed: Ready for UPS",
                            "Code": "MP"
                        },
                        "Date": "20171129",
                        "Time": "193858"
                    }
                ],}

Here are my classes:

public class Address
{
    public string City { get; set; }
    public string StateProvinceCode { get; set; }
    public string CountryCode { get; set; }
}

public class ActivityLocation
{
    public Address Address { get; set; }
}

public class Status
{
    public string Type { get; set; }
    public string Description { get; set; }
    public string Code { get; set; }
}

public class Activity
{
    public ActivityLocation ActivityLocation { get; set; }
    public Status Status { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }
}

public class RootObject
{
    public List<Activity> Activity { get; set; }
}

var huh = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
MessageBox.Show(huh[0].Activity[0].ActivityLocation.Address.City);

I expect to get back a string value for city

I get the null reference error.

The exact error is:

System.NullReferenceException: Object reference not set to an instance of an object. at UPSTracking.UPSTrack.<>c_DisplayClass1.b_()(IRestResponse response)

losrob
  • 115
  • 1
  • 7
  • Could you please copy the full error message into your question? There might be some helpful information in it. – Pac0 Dec 02 '17 at 23:32
  • are you deserializing the correct object I see RootObject, perhaps it should be List object – MethodMan Dec 02 '17 at 23:33
  • 1
    So, that means that you checked the debugger when you are saying that *everything looks ok to me*? To me it looks like your Json, starts with an object, and isn't a list of objects – Icepickle Dec 02 '17 at 23:33
  • RootObject is not a collection – Ňɏssa Pøngjǣrdenlarp Dec 02 '17 at 23:34
  • @MethodMan That did the trick!!!! Thanks if you submit it as an answer I can mark yours as the answer!....I figured the RootObject would iterate and place everything into place! I was wrong Thanks!!! – losrob Dec 02 '17 at 23:39
  • @losrob not a problem, I saw it immediately glad I could help you quickly fix this issue. – MethodMan Dec 02 '17 at 23:44

1 Answers1

0

you do have no RootObject based on your JSON payload I saw immediately that you had { Activity: [..... so you need to change your code to the following

var huh = JsonConvert.DeserializeObject<List<Activity>>(jsonString);
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • So, in essence, the single RootObject he had before? – Icepickle Dec 02 '17 at 23:43
  • @Icepickle if you go to this link http://json2csharp.com/ change the `"Activity":` to `"RootObject"`it will create `RootObject2` this is not the same in essence. – MethodMan Dec 02 '17 at 23:57