0

Could you please let me know how to extract JSON data received in a string variable in controller. Please see the attachment.Thanks.

image showing json data received in controller

 $("#btn1").on("click", function () {
    var i = new Array();
    var j = 0;
    $("#sl1").multiselect("getChecked").map(function () {
        alert(this.value);
        i.push(this.value);
        //i[j] = this.value;                     
        //j++;

    }).get();
    var postData = { values: i };
    jQuery.ajaxSettings.traditional = true;
    $.post('/TodoList/searchdata', postData, function (data) {

        alert(data.Result);
    });
    //$.ajax({
    //    type: "POST",
    //    url: "/TodoList/searchdata",
    //    data: postData,
    //    success: function (data) {
    //        alert(data.Result);
    //    },
    //    dataType: "json",
    //    traditional: true
    //});

});

Controller code:-

public void searchdata(String[] values)
    {
    //{
    //    JavaScriptSerializer js = new JavaScriptSerializer();
    //   List<String[][]> data=js.Deserialize<List<String[][]>>(i);
        Console.WriteLine(values);

    }
Baji
  • 131
  • 5
  • 16
  • You need to show your code (including the ajax). And you should be binding it to a model (not a `string`) –  Jan 16 '16 at 04:57
  • $("#btn1").on("click", function () { var i = new Array(); var j = 0; $("#sl1").multiselect("getChecked").map(function () { alert(this.value); i.push(this.value); //i[j] = this.value; //j++; }).get(); var postData = { values: i }; jQuery.ajaxSettings.traditional = true; $.post('/TodoList/searchdata', { i: postData }, function (data) { alert(data.Result); }); – Baji Jan 16 '16 at 04:59
  • Edit your question, not in comments (impossible to read when you dont even format it). And include the controller signature (and delete the image) –  Jan 16 '16 at 05:00
  • I did that. Please find the attached code. – Baji Jan 16 '16 at 05:03
  • It appears your passing an array of strings so it should be `$.post('/TodoList/searchdata', postData, function (data) {` and the parameter should be `string[] values` –  Jan 16 '16 at 05:03
  • it didn't help. Do you have another solution? – Baji Jan 16 '16 at 05:11
  • I will create a Fiddle for your shortly –  Jan 16 '16 at 05:13
  • Thank you very much Stephen. It really helps me. – Baji Jan 16 '16 at 05:15
  • Just test the code from my previous comment and it works for me. Did you change the `$.post()` code as per my last comment as well? - the parameter is `values` is correctly bound with the array. –  Jan 16 '16 at 05:22
  • another question asked today: http://stackoverflow.com/questions/34823400/parsing-complicated-json-array-in-c-sharp/34823448#34823448 seems everybody is into parsing today :) – naveen Jan 16 '16 at 05:24
  • Please see my changed code @Stephen – Baji Jan 16 '16 at 05:31
  • I have edited code please let me know – Baji Jan 16 '16 at 05:33

3 Answers3

0

You can use Newtonsoft Json library https://www.nuget.org/packages/Newtonsoft.Json/

So As mentioned in the below link use it like below

string json = @"{ 'Email': 'james@example.com', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z', 'Roles': [ 'User', 'Admin' ] }";

Account account = JsonConvert.DeserializeObject(json);

if you doesn't have model , just use like below

var model = JsonConvert.DeserializeObject(json);

the check the below link

http://www.newtonsoft.com/json/help/html/deserializeobject.htm

Midhun Murali
  • 2,089
  • 6
  • 28
  • 49
0

Try this

JavaScriptSerializer js = new JavaScriptSerializer();
var data=js.Deserialize<Dictionary<string, List<string>>>(i);
naveen
  • 53,448
  • 46
  • 161
  • 251
0

Use This Class :

public class JsonAttributeClass<T> where T:class ,new()
{
   public static string EntityToJsonConvertor(T entity)
   {
       string json = JsonConvert.SerializeObject(entity);
       return json;

   }

   public static T JsonToEntityConvertor(string json)
   {
       var entity = JsonConvert.DeserializeObject<T>(json);
       return entity;
   }
}
Amin Seifi
  • 58
  • 1
  • 6