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?

2
  • 1
    should MessageTest be named TestMessage in public class MyClass? Commented Jul 11, 2018 at 5:39
  • Yes it shold be public and for data display get from the API I display that messge. Commented Jul 11, 2018 at 5:42

2 Answers 2

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; }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

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.
Hi Deep. can you share your code for converting json data to class?
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
Hi Deep. first get response into string. then after try this MyClass MyClassResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(yourresponse);
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;}

4 Comments

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
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.
Thanks for answer I try with FiringSquadWitness with solution and tell you problem arise or not.
I try with option you suggested but it doesn't works for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.