0

I am calling API and get this response from the API.

{
    "StringValue": "Hi Developers",
    "TestMessage": {
        "": [
            "Enjoy Coding."
        ]
    }
}

This is how I tried to make my C# class.

public class MyClass
{
  public string StringValue{ get; set; }
  public TestMessage MessageTest { get; set; }
}
public class TestMessage 
{
  [JsonProperty("")]
  public IList<string> APIResponse{ get; set; }
  //public IList<string> { get; set; }//Also try this but it gives me compilation error
}

but It didn't work how can I get this to work?

Zeeshan Adil
  • 1,937
  • 5
  • 23
  • 42
Deep Soni
  • 431
  • 4
  • 24

2 Answers2

1

can u try this

public class MyClass
    {
      public string StringValue{ get; set; }
      public TestMessage MessageTest { get; set; }
    }
    public class TestMessage 
    {
      [JsonProperty("")]
      public string[] APIResponse{ get; set; }
    }
  • Hi Pradip. Firstly I try with this but it goes to `Catch` part and throw a exception of `Object not set to an instance of an object`. – Deep Soni Jul 11 '18 at 06:05
  • Hi Deep. can you share your code for converting json data to class? – Pradip Meghapra Jul 11 '18 at 06:14
  • Yes sure Please check this it's get call from webApi and I try to call it on load `MyClass MyClassResponse = await apiGetCall.MyAPICall();` Can you please suggest me – Deep Soni Jul 11 '18 at 06:20
  • Hi Deep. first get response into string. then after try this MyClass MyClassResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(yourresponse); – Pradip Meghapra Jul 11 '18 at 06:35
0

The array is incorrect. Try "TestMessage": ["Enjoying Coding.", "Something else"]. You're creating an empty element so it's confusing it. For your object, it's named something different as well. MessageTest should be TestMessage. Lastly, for your actual IList<string>,

try public IList<string> Messages {get;set;}

Derrick
  • 3,669
  • 5
  • 35
  • 50
Steven Mayer
  • 641
  • 1
  • 6
  • 19
  • HI Steven Actually this is not set by my side it already exists and now from other technology I migrate it to ASP.NET that's why I am not able to change message response – Deep Soni Jul 11 '18 at 05:43
  • 1
    In that case, FiringSquadWitness is correct - Change the name of the property in MyClass from MessageTest to TestMessage and it should bind onto the correct property. – Steven Mayer Jul 11 '18 at 05:45
  • Thanks for answer I try with FiringSquadWitness with solution and tell you problem arise or not. – Deep Soni Jul 11 '18 at 05:59
  • I try with option you suggested but it doesn't works for me. – Deep Soni Jul 11 '18 at 06:12