0

I have this json:

{ "Message": "The request is invalid.", "ModelState": { "Email": [ "The Email field is required." ] } }

I want to find the ModelState(if it exists) and then loop through all the errors that re in there.

I can figure out how to do this. I don't want to make a concrete class as the data might change depending on what happens on the sever.

I also can use dynamic as I am on WPF7

          JObject jsonObj = JObject.Parse(response.Content);

            foreach (var j in jsonObj)
            {
               var t = j.Value;

            }

this is what I have so far.

1 Answer 1

1
JObject jsonObj = JObject.Parse(response.Content);
var modelState = jsonObj["ModelState"];
if (modelState != null)
{
    // The JSON contains a property called ModelState
    // so we can start looping through it:
    foreach (JProperty item in modelState)
    {
        Console.WriteLine(item.Name);
        foreach (JValue error in item.Values())
        {
            Console.WriteLine(error);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.