I can't deserialize json array

i have input json

{
    "StudentData": {
        "Students": [{
            "StudentId": 76769,
            "StudentName": "*****",
            "Contacts": [{
                "ContactName": "****",
                "ContactEmail": "****",
                "АvailableContactEmail": true,
                "ContactMobile": "****",
                "АvailableContactMobile": true
            }]
        }]
    }
}

and this code

public class Contacts
{
    public string ContactName { get; set; }
    public string ContactEmail { get; set; }
    public bool АvailableContactEmail { get; set; }
    public string ContactMobile { get; set; }
    public bool АvailableContactMobile { get; set; }
}

public class Students
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public List<Contacts> Contacts { get; set; }
}

public class StudentData
{
    public List<Students> Students { get; set; }
}

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

       // Get request body
        dynamic data = await req.Content.ReadAsAsync<object>();
        var stuData = JsonConvert.DeserializeObject <StudentData>(data?.StudentData.ToString());

        RootObjectNew RootObjectNewObject = new RootObjectNew();
        List<StudentNew> students = new List<StudentNew>();

        foreach (var stu in stuData.Students)
        {
            var stuContacts = JsonConvert.DeserializeObject <List<Contacts>>(stu?.Contacts.ToString());
        }
}

but I can't get contacts. Get error

Unexpected character encountered while parsing value: S. Path '', line 0, position 0.

Please, help to make the correct analysis of the structure. I looked through many similar topics, but they did not help me

Upvotes: 0

Views: 168

Answers (2)

tchelidze
tchelidze

Reputation: 8308

data?.StudentData.ToString() will not give you JSON representation of object, it just displays it's name.

Try following

public class StudentDataContainer 
{
    public StudentData StudentData { get; set; }

}

var data = await req.Content.ReadAsAsync<StudentDataContainer>();

Upvotes: 2

styx
styx

Reputation: 1917

i would suggets these classes

    public class Contact
    {
        public string ContactName { get; set; }
        public string ContactEmail { get; set; }
        public bool АvailableContactEmail { get; set; }
        public string ContactMobile { get; set; }
        public bool АvailableContactMobile { get; set; }
    }

    public class Student
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
        public List<Contact> Contacts { get; set; }
    }

    public class StudentData
    {
        public List<Student> Students { get; set; }
    }

    public class RootObject
    {
        public StudentData StudentData { get; set; }
    }

and then you can simply do this

     RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

Upvotes: 0

Related Questions