locked
Circular Reference json serialize object in c# mvc RRS feed

  • Question

  • User615038210 posted

    Hi,

    i want to get data in json format. but i'm getting response in string format so how can i resolve  this  issue,

     [HttpGet]
            public JsonResult DriverLogin(string username, string password, string GcmId)
            {
                _db.Configuration.ProxyCreationEnabled = false;
                var result = new
                {
                    controller = "mobile",
                    action = "DriverLogin",
                    status = "OK"
                };
    
                try
                {
                    var driver = _db.DRVR_DATA.Where(d => d.LoginName == username && d.Password == password && d.IsDeleted == false).FirstOrDefault(); //&& d.IsLogOut == true
    
    
                    if (driver != null)
                    {
                        driver.IsActive = true;
                        driver.LoginTime = DateTime.Now.ToUniversalTime();
                        driver.GcmId = GcmId;
                        driver.IsLogOut = false;
                        _db.Entry(driver).State = EntityState.Modified;
                        _db.SaveChanges();
    
    
    
                        SaveAuthsession(driver.DriverId);
                        var authData = _db.AuthorisedSessions.Where(d => d.DriverId == driver.DriverId).FirstOrDefault();
                        result = new
                        {
                            controller = "mobile",
                            action = "DriverLogin",
                            status = "OK"
                        };
    
    
    
                        var response = new
                        {
                         result,
                          driver,
                          authData
                        };
    
    
                        return Json(response, JsonRequestBehavior.AllowGet);
                        
    
                    }
                    else
                    {
                        var error = "Invalid User Name & Password ";
                        result = new
                        {
                            controller = "mobile",
                            action = "DriverLogin",
                            status = "NOK"
                        };
                        var response = new
                        {
                            result,
                            error
                        };
                        return Json(response, JsonRequestBehavior.AllowGet);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    
    
                }
            }
    
    

    Monday, February 13, 2017 1:51 PM

All replies

  • User-1509636757 posted

    It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer.

    Monday, February 13, 2017 4:31 PM
  • User615038210 posted

    how can i'm  resolve this issue please let me know 

    Monday, February 13, 2017 6:24 PM
  • User-832373396 posted

    Hi balvinder,

    i want to get data in json format. but i'm getting response in string format so how can i resolve  this  issue,

    var response = new { result, driver, authData }; return Json(response, JsonRequestBehavior.AllowGet);

    Based on your picture, I find {"e":1,"t":2} is JSON string and it is JSON format.

    please refer to some JSON examples below:

    1:{ "firstName": "Brett" } 
    2:{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }
    3:{ "people": [
    { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
    
    { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
    
    { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
    
    ]}

    Best regards,

    Jolie

    Tuesday, February 14, 2017 6:36 AM