1

I am trying to read Rest API, everything works fine and I am getting data back from api but it is failing on line where it says if (response.Data.result == null). I am getting an error saying 'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'result'. Any help will be highly appreciated. Here is the code:

        try
        {
            var response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode.ToString() == "OK")
            {
                var responseStream = response.GetResponseStream();
                StreamReader _answer = new StreamReader(responseStream);
                string result = _answer.ReadToEnd();
                var responseData = JsonConvert.DeserializeObject<dynamic>(result);



                if (responseData.result == null || String.IsNullOrEmpty(responseData.result.ToString()))                    
                    return null;
                else
                {
                    var contactData = JsonConvert.DeserializeObject<Contact>(responseData.result.ToString());
                    return contactData;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
Mr_Roomi
  • 11
  • 1
  • 2
  • Is there a particular reason you are deserialising to `dynamic` rather than a concrete type? – mjwills Jun 18 '18 at 13:01
  • mjwills, I inherited this code and trying to make it work...I am open to any other solutions... – Mr_Roomi Jun 18 '18 at 13:04
  • If you're stuck using the `dynamic` type then this might help: https://stackoverflow.com/questions/2998954/test-if-a-property-is-available-on-a-dynamic-variable - otherwise look to use concrete types as suggested – Equalsk Jun 18 '18 at 13:17
  • I assume you mean "it is failing on line where it says if (responseData.result == null)" - your original refers to `response.Data.result` (note the extra `.`). I don't want to edit it myself in case my assumption is wrong! – Chris Jun 18 '18 at 13:18
  • Chris, Yes, I meant to say responseData. result – Mr_Roomi Jun 18 '18 at 13:19

1 Answers1

0

You can check directly if the parsed object is null or not:

if (responseData == null && String.IsNullOrEmpty(responseData.ToString()))       
                return null;
Bassel Shmali
  • 207
  • 2
  • 15
  • Bassel, with your response, I think I have figured it out, I removed the work result and used the following line which resolved the issue responseData[0] == null I will do some more testing to see if it breaks at any point...thanks – Mr_Roomi Jun 18 '18 at 13:22
  • I am getting this error now.... Error converting value \"8724c022-1276-4a58-9efe-1c566a925552\" to type 'System.Int64'. Path 'UserUid' – Mr_Roomi Jun 18 '18 at 13:32
  • you're converting a non-integer string to integer, `UserUid` should string not int @Mr_Roomi – Bassel Shmali Jun 21 '18 at 12:20