0

i am trying to parse json datalist in ajax success bt it couldnot work. i have passed object containing list of data using json from controller to view,i help to parse json object in ajax success. i have attached my code below.

//return json object from controller

 PurchaseDetails pd = new PurchaseDetails();
   foreach (DataRow dr in dt.Rows)
                    {
                        pd.serialList.Add(new SerialInfo { 
                            Machine_serial_no = dr[0].ToString(), macAddress = dr[1].ToString(), isMacPresent = _TD1.CheckMac(machineTypes_MTId),brandName=obj.brandName,machineName=obj.machineName,MachineModel=obj.MachineModel,modelId=modelId,machineId=obj.machineId,brandId=obj.brandId});

                        // pd.macaddressList.Add(new MacAddressInfo { MacAddress = dr[1].ToString() });
                    }                   
                }
            }
            return Json(new {pd}, JsonRequestBehavior.AllowGet);
     return Json(new {pd}, JsonRequestBehavior.AllowGet);

// my ajax code

$.ajax({
    url: "/Import/ImportPurchase",
    type: "POST",
    data: function () {
        var data = new FormData();
        data.append("file", jQuery("#file").get(0).files[0]);                                 
        data.append("machineTypes_MTId", jQuery('#machineTypes_MTId').val());
        data.append("modelId", jQuery('#searchValue').val());
        data.append("modelName", jQuery('#searchid').val());
        return data;
    }(),
    dataType:"JSON",
    contentType: false,
    processData: false,
    success: function (data) {                        
        alert(data.Machine_serial_no)
it'sME
  • 3
  • 1
  • 6
  • In your success callback, data is an object with a property named `serialObj` which is your list (its not clear why you don;t just use `return Json(pd, JsonRequestBehavior.AllowGet);` instead. But if `pd` is a list then you will need to loop through it. –  Jul 31 '15 at 05:02
  • @StephenMuecke yes exactly that is what i want to do bt as i am a learner i cant parse that pd object in my ajax sucess , can u help ? – it'sME Jul 31 '15 at 05:28
  • You need to show what `pd` is. If its a collection, then show the model in the collection –  Jul 31 '15 at 05:31
  • I think your JSON object construct is wrong. It should be `return Json(new { serialObj : pd }, JsonRequestBehavior.AllowGet);` – Anand Jul 31 '15 at 05:56
  • @Anand, No. OP's usage is correct. –  Jul 31 '15 at 06:09
  • @Anand i just recorrect my json object as return Json(pd, JsonRequestBehavior.AllowGet); – it'sME Jul 31 '15 at 06:11
  • @StephenMuecke 'pd' is a model object containg serial_list – it'sME Jul 31 '15 at 06:12
  • Which part about _"you need to show the model"_ do you not understand? Edit your question to show what `pd` is. –  Jul 31 '15 at 06:14
  • @StephenMuecke i 've just edit my question . hoping your help – it'sME Jul 31 '15 at 06:22

3 Answers3

0

you can use jQuery.parseJSON() to parse ajax response to JSON.

but seeing from your code, seems the response should already be in JSON because dataType parameter is already set to "JSON".

try this:

  • remove "()" after the ending bracket of function in "data" parameter.
  • try to console.log(data) and look at the response from your browser console, the response might not be a valid json string.
VMcreator
  • 833
  • 8
  • 17
0

If you are looking for convert js array to JSON then just use JSON.stringify() function. This will convert your JS variable data value to JSON format.

You can more find here.

Community
  • 1
  • 1
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
0

Your controller method is returning

return Json(new { serialObj = pd}, JsonRequestBehavior.AllowGet);

which is an object containing a name serialObj

So in the ajax success call back, you would need to access it using

success: function (data) {
  var PurchaseDetails = data.serialObj;

Since PurchaseDetails contains a collection named serialList which is a collection of SerialInfo, then to access the value of the first Machine_serial_no, it would be

var serialNo = data.serialObj.serialList[0].Machine_serial_no;

However it would be easier to just use

return Json(pd, JsonRequestBehavior.AllowGet);

If you then want to access each Machine_serial_no property in the collection, use

success: function (data) {
  $.each(data.serialList, function(index, item) {
    var serialNo = item.Machine_serial_no;