0

I have dictionary which is nested as shown below:

enter image description here

I want to convert this object to JSON which will be used to send to controller. I have tried to convert the same to array but nothing worked for me.

array = Object.keys(dicCoachList).map(function (k) {
    return dicCoachList[k];
});

Which I send to controller using ajax.

$.ajax({
        url: '/Dispatch/SavePatientCoaching/',
        data: { careProfessional : array },
        type: "POST",
        success: function (data) {

        },

In controller like this.

[HttpPost]
public ActionResult SavePatientCoaching(string[] careProfessional)
{
}

If I convert my object to json it just return "{"1":[],"2":[]}" So I used array. Please help me send this data to controller.

3
  • To turn an object into JSON, use JSON.stringify? Commented Feb 10, 2019 at 11:50
  • I already tried that as well var careProfessional = JSON.stringify(dicCoachList); which returns "{"1":[],"2":[]}" Commented Feb 10, 2019 at 11:54
  • Your Action is expecting an array of strings (eg. ['foo', 'bar']) yet you're sending a dictionary of key/value pairs. One of them needs to be changed to the correct format. Also note that once you've fixed that you don't need to wrap the values in an object when sending from $.ajax; just data: array will work. Commented Feb 10, 2019 at 12:21

1 Answer 1

1

Your dictionary dicCoachList is not valid for converting to a JSON, you must use an object instead of an array for (CouchId etc).

Example of correct output format:

let dicCoachList = {
    data:[
      {CouchId:"test1", CareTeamId:"test1"},
      {CouchId:"test2", CareTeamId:"test2"},
      {CouchId:"test3", CareTeamId:"test3"}
    ]
};
      
console.log( JSON.stringify(dicCoachList));

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.