8

I have the following json Object that I pass to my c# server

[
    {
        "ID": 1,
        "FirstName": "Jay",
        "LastName": "Smith"
    },
    {
        "ID": 2,
        "FirstName": "Rich",
        "LastName": "Son"
    },
    {
        "ID": 3,
        "FirstName": "Emmy",
        "LastName": "Wat"
    }
]

I create a Class like this

public class Person
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

When I do this

public static string addRecord(string details)
{
    Person tempRecord = JsonConvert.DeserializeObject<Person>(details);
    string tempFN = tempRecord.FirstName;
    return tempFN;
}

I can't get the actual result.

What am I doing Wrong? Do I have to make another List in my Person class? Any help?

UPDATE - my record is from Grid and this is how I send it to my server

    var jsonD = Ext.encode(Ext.pluck(this.myGridStore.data.items, 'data'));
    Ext.Ajax.request({
        scope: this,
        method: 'POST',
        url: 'myApp/AddRecord',
        headers: { 'Content-Type': 'application/json' },
        dataType: 'json',
        jsonData: jsonD,
        success: function (response) {
        },
        failure: function (response) {
        }
    });
0

1 Answer 1

17

Your JSON contains a collection of three Persons, but you're attempting to deserialize the JSON as though it were a single Person.

Person tempRecord = JsonConvert.DeserializeObject<Person>(details);

This line needs to return a collection of Persons.

var tempRecords = JsonConvert.DeserializeObject<List<Person>>(details);
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.