javascript - How to deserialize multiple JSON objects in C#? -


i'm passing multiple json objects frontend c# backend - how can deserialize them c# classes can used later on in application? before go farther, tied using js formdata object, contenttype: false, , processdata: false, because need pass files via ajax call; unrelated question. here's code far:

frontend - function called when submit button pressed

submitdata: function () {     var formcollection = this.appmodel.get('formcollection').models;     var formdata = new formdata();     var formjson = [];     $.each(formcollection, function (index, form) {         var tempjson = {};         if (form) {             tempjson['id'] = form.get('id');             tempjson['number'] = form.get('number');             tempjson['name'] = form.get('name');             tempjson['attachments'] = form.get('attachments');             formjson.push(tempjson);         }     });     console.log(json.stringify(formjson));     formdata.append('formjson', json.stringify(formjson));     $.ajax({         type: "post",         url: '/nmisproduct/test',         contenttype: false,         processdata: false,         data: formdata,         success: function (result) {             console.log(result);         }     }); } 

json.stringified data that's being passed

[{"id":1,"number":null,"name":"investment portfolio statement","attachments":null},{"id":2,"number":"61-0227","name":"wmc signature portfolio agreement","attachments":null},{"id":3,"number":"61-1126","name":"wmc signature choice agreement","attachments":null},{"id":4,"number":"61-1162","name":"wmc signature annuities agreement","attachments":null},{"id":5,"number":"61-1205","name":"wmc signature managed accounts client agreement","attachments":null}] 

c# mvc 5 backend

[httppost] public async task<jsonresult> test() {     debug.writeline(request.params["formjson"]);     var forms = jsonconvert.deserializeobject<form>(request.params["formjson"]);     debug.writeline(forms);     try     {         var srnumber = getsrnumber();         foreach (string file in request.files)         {             var filecontent = request.files[file];             debug.writeline(readfileinputstream(filecontent));             if (filecontent != null && filecontent.contentlength > 0)             {                 // stream                 var stream = filecontent.inputstream;                 // , optionally write file disk                 //var filename = path.getfilename(file);                 var filename = filecontent.filename;                 var path = path.combine(server.mappath("~/app_data/"), filename);                 using (var filestream = system.io.file.create(path))                 {                     stream.copyto(filestream);                 }             }         }     }     catch (exception)     {         return json("upload failed");     }     return json("file uploaded successfully"); }  public class form {     public int id { get; set; }     public string number { get; set; }     public string name { get; set; }     public form attachments { get; set; }     public byte[] fileasbytes { get; set; } } 

i've done research , found several stackoverflow questions show how serialize 1 json object 1 c# class - however, need serialize multiple json objects list<form>. how can this? doing wrong in posted code?

you're trying deserialize single form here:

var forms = jsonconvert.deserializeobject<form>(request.params["formjson"]); 

just change to:

var forms = jsonconvert.deserializeobject<list<form>>(request.params["formjson"]); 

you should able use forms in normal way - iterating on it, taking count etc.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -