I have a problem, I cannot add custom objects to my List. However, if I add some predefined objects (strings, integers..) my code works just fine.
This is my class which is always returned on all ajax calls to this controller, it contains boolean Status and List of objects.
public class returnData
{
public bool status { get; set; }
public List<object> data { get; set; }
}
This is the class for the object I want to add to this list:
public class file
{
public string ID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
The code where I use this:
returnData data = new returnData();
...
DriveFile file = JsonConvert.DeserializeObject<DriveFile>(Json);
file f = new file();
if (file.mimeType == "application/vnd.google-apps.folder")
{
f.Type = "folder";
f.ID = file.id;
f.Name = file.title;
data.data.Add(f);
}
The error I get has no useful info, as this code compiles and the error is in Chrome Console log when the ajax is executed.
Image of google chrome console
However, when I use this line of code
data.data.add("some string");
my function will work flawlessly and return Object which contains boolean Status and List of objects (strings, integers..)
So back to my question, how do I add my objects to this list?
EDIT: There are no server side errors, the code compiles and runs just fine, so this probably is some jquery/ajax error.
This is the ajax code for my debugger:
86 $.ajax({
87 url: $('#url').val(),
88 type: $('#type option:selected').val(),
89 contentType: "application/json; charset=utf-8",
90 dataType: "json",
91 data: $('#data').val(),
92 success: function (result){
93 console.log(result)
94 },
95 error: function (err){
96 console.log(err)
97 }
98 });
Here is the complete method, but the method seems to be OK, it is the ajax or something to do with jquery I guess.
[OperationContract]
public returnData listChildren(string folderId)
{
returnData data = new returnData();
List<object> myList= new List<object>();
bool status = true;
try
{
string Json = Response("https://www.googleapis.com/drive/v2/files/" + folderId + "/children", "GET");
//get the children list
if (!Json.Contains("error"))
{
DriveChildlist list = JsonConvert.DeserializeObject<DriveChildlist>(Json);
foreach (DriveChildren child in list.items)
{
string uriString = child.childLink;
//get the info for each child in the list
Json = Response(uriString, "GET");
if (!Json.Contains("error"))
{
DriveFile file = JsonConvert.DeserializeObject<DriveFile>(Json);
file f = new file();
if (file.mimeType == "application/vnd.google-apps.folder")
{
f.Type = "folder";
f.ID = file.id;
f.Name = file.title;
data.data.Add(f);
}
else if (file.mimeType == "application/myMimeType")
{
f.Type = "file";
f.ID = file.id;
f.Name = file.title;
data.data.Add(f);
}
}
else
{
status = false;
}
}
}
else
{
status = false;
}
}
catch(Exception)
{
status = false;
}
data.status = status;
if (!status)
{
data.data = null;
//data.data is null if status is false
}
return data;
}
As written your C# code won't work but will compile. It will throw an error when the ajax is called and you may never see the error message (depending on how your system is setup).
Here is correct code (given your snippet)
returnData data = new returnData();
data.data = new List<object>();
// rest of your code.
I have solved this using the Newtonsoft.Json. I have serialized the object using this code:
string jsondata = JsonConvert.SerializeObject(data);
and then I returned the serialized string instead of object. Looks like visual studio development center failed to serialize my object, even after adding [Serializable()] to my class. Any info on that?
Related
I have a webservice that returns json array (JArray) in string format, but I do not understand how to add state value to that operation and get it in the application that consumes the service.
My question is, should I return a json object with a message and inside a json array? Or just an array? hich is more convenient ?
my ws:
public string getList(string strSalary)
{
List<Employee> listJson = null;
JObject jsonResp = "";
JArray array = null;
try
{
listJson = ReportBLL.getInstance.listEmployees(int.Parse(strSalary));
array = JArray.FromObject(listJson);
//set array status ?: ej
//array status = "succes";
}
catch (Exception ex)
{
//set error message
// array status = "error";
//array message = ex.Message or "Not found employees";
}
return JsonConvert.SerializeObject(array);
}
client call (other asp app):
public static List<Employee> listEmployeeClient(string salary)
{
JObject jo = null; //or arrayjson ?
string strData = "";
strData = webService.getList(salary);
jo = JObject.Parse(strData);
//how to evalue status request ?
/* example
if(jo.status == "error") {
throw new Exception(jo.message);
} else {
iterate array inside json object ?
}
*/
}
Is this logic correct ?
You can create a new entity for API Response and use it for all your API responses, You can test it out by the following example.
In server:
Class APIResponse<T>
{
public bool IsError;
public int ErrorCode;
public string ErrorMessage;
public T ReponseData;
}
public string getList(string strSalary)
{
List<Employee> listJson = null;
APIResponse<Employee> responseString = new APIResponse<Employee>();
try
{
listJson = ReportBLL.getInstance.listEmployees(int.Parse(strSalary));
responseString.isError = false;
responseString.data = JArray.FromObject(listJson);
}
catch (Exception ex)
{
responseString.IsError = true;
responseString.ErrorCode = 404; //You can add custom error codes
responseString.ErrorMessage = ex;
}
return JsonConvert.SerializeObject(responseString);
}
In Client:
public static List<Employee> listEmployeeClient(APIResponse<Employee> salary)
{
//You can access the model here
}
There are many options. If you have REST apis you can use the HTTPStatusCodes, for each request, e.g. 200 for OK, 400 bad request, etc.
If you want more fine tuning, you can have a general structure of your response objects, e.g.
responseDto:
status: any code, error or success
message: any error message
data: any expected data
use a Dictionary<string,object> before serializing, and use dynamic to conveniently get the various fields after deserializing. jArray.ToObject<List<Employee>>() will convert JArray object back to the proper type.
Example below:
class Employee
{
public string Name { get; set; }
}
// serializing
var employees = new List<Employee>()
{
new Employee() {Name = "john"},
new Employee() {Name = "alex"},
new Employee() {Name = "susan"},
new Employee() {Name = "bryan"},
};
var dict = new Dictionary<string, object>
{
["employees"] = employees,
["status"] = "error",
["errormessage"] = "Not found employees"
};
var json = JsonConvert.SerializeObject(dict);
// deserializing
dynamic deserialized = JsonConvert.DeserializeObject(json);
string status = deserialized.status;
string errorMessage = deserialized.errormessage;
JArray jArray = deserialized.employees;
List<Employee> deserializedEmployee = jArray.ToObject<List<Employee>>();
I try to return Entity Framework Objects as Json with the following method in my Controller:
public JsonResult EventList() {
var results = from s in db.Events
select new
{
OrderID = s.EventID,
OrderTitle =s.EventType,
OrderDate = s.Title
};
return Json(results);
}
I get a server error 500 when entering the page /events/EventList/. Also a Jquery get request returns no data. What is the correct way to return the results in Json format?
Update:
This seems to work. But I need results from the database.
public ActionResult EventList() {
Event test = new Event
{
EventID = 1,
Title = "test",
Description = "test"
};
return Json(new { event = test }, JsonRequestBehavior.AllowGet);
}
Edit: 2019
This answer still gets upvotes - if you're going down this road I really really suggest you just save yourself the future headaches and use a DTO. Serializing your entity is probably faster for now but (and I have learned this the hard way) - you are going to hate yourself in a years time.
New answer - updated for 2018:
The original answer below will work every time and if you're using lazy loading, it may still be the best solution. Without lazy loading though, you can do the following with Newtonsoft.JSON:
var settings = new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Error = (sender, args) =>
{
args.ErrorContext.Handled = true;
},
};
using(var context = new myContext())
{
var myEntity = myContext.Foos.First();
return JsonConvert.SerializeObject(myEntity, settings);
}
Which basically will serialize anything that's been included in the entity framework request, while ignoring any errors and reference loops.
The drawback to this method is that controlling what gets serialized is harder and if you're performance conscious, you may need to start decorating your generated Entity Framework classes with a pattern like
// a new partial class extending the Foo generated class, allowing us to apply an interface
[MetadataType(typeof(IFooMetaData))]
public partial class Foo : IFooMetaData
{
}
// meta data interface, forcing json ignore on Foo.Bars
public interface IFooMetaData
{
[JsonIgnore]
ICollection<Bar> Bars {get;set;}
}
Original answer - 2015:
Can get this response:
[
{
"OrderID": 1
},
{
"OrderID": 2
},
{
"OrderID": 3
}
]
from this:
public JsonResult Test()
{
var events = new List<Event>()
{
new Event() {EventId = 1},
new Event() {EventId = 2},
new Event() {EventId = 3}
};
var results = events.Select(e => new
{
OrderID = e.EventId
}).ToList();
return new JsonResult() { Data = results, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
So yours should be something like
public JsonResult Test()
{
var results = db.Events.Select(e => new
{
OrderID = e.EventId
}).ToList();
return new JsonResult() { Data = results, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
edit
re-posted with tested code
In the controller:
for example
List<categories> data = context.categories.toList(); //to here all right
now, the problem with the serialization for "data" for send the json result...
i just used a struct to replace the List how???
just watch:
in the same controller ...
struct categories_struct{
public fieldname {get;set;}
//the same for others firelds (same the model)
}
now, in the ActionResult or maybe in jsonresult :
List<categorias> data = contexto.categorias.ToList(); //I use the EF
List<catego> data2 = new List<catego>(); //create the struct var
foreach (categorias item in data) //fill the struct with data
{
catego item2 = new catego();
item2.codcat = item.codcat;
item2.nomcat = item.nombrecat;
item2.descripcion = item.descripcion;
data2.Add(item2);
}
//here, Data contains all data2
return Json(new { Data = data2 }, JsonRequestBehavior.AllowGet);
in the view:
$.ajax({
url: "/Categorias/getTabla",
dataType: "JSON",
type: "POST",
}).done(function (respuesta) {
var data = JSON.parse(JSON.stringify(respuesta));
console.log(respuesta);
alert('exito');
}).fail(function () {
alert('oh no, again!');
});
///
is my solution for this problem
Im creating some javascript items in a loop
var licenseList = {};
$($licenses).each(function (index) {
var license = {};
var editedValues = {};
license.PRODUCT_KEY = $(this).parent('div.licensewrapper').data('productkey');
$(this).find('input:text').each(function (i, val) {
if ($(val).attr('data-default-value') != $(val).val() && $(val).val() > 0 && $(val).data('isValid') != false) {
var pogKey = $(val).data('product_option_group_key');
var editedValue = $(val).val();
editedValues[pogKey] = editedValue;
license.editedValues = editedValues;
}
});
//licenseList[index] = license;
//liceneList.push(license); //if array...
});
I've commented out my current solutions. But i dont think any of the two are equal to a generic list when derelializing them in c#. Whats the corrent way to do it in this case? Thanks
create your array
var licenseList = [];
for each of your licenses...
var license = {
prop1: 'p1',
prop2: 'p2'
};
licenseList.push(license);
format and serialize JSON data to be sent over to webmethod
data = {
LicenseList: licenseList
};
$.ajax({
...
data: JSON.stringify(data)
...
});
in your webmethod your method parameter must match
[WebMethod]
public static string GetLicenses(List<License> LicenseList)
{
foreach(var license in LicenseList)
{
// do whatever
}
}
sample license class. Your properties need to match with your objects in your javascript
public class License
{
public string Prop1 {get; set;}
public string Prop2 {get; set;}
}
Hope this helps.
function sendToServer(licenseList)
{
var url = "controller.ashx";
var data = {};
data.SerializedObj = JSON.stringify(licenseList);
$.post(url, data, function(response){
if(response.length > 0)
{
alert(response);
}
});
}
//controller.ashx :
public void ProcessRequest (HttpContext context) {
//...
string serializedObj = context.Request.Form["SerializedObj"] ?? "";
JavaScriptSerializer js = new JavaScriptSerializer();
List<license> collection = js.Deserialize<List<license>>(serializedObj);
//...
public class license
{
public string Propertie1 {get;set;}
public string Propertie2 {get;set;}
}
Javascript object must have the same properties:
var license = {Propertie1 : 'value1', Propertie2 : 'value2'};
I have an Ajax call (for a HighChartsDB chart) in a WebForm application that calls a webservice, which uses a WebRequest to call an MVC action in another application which returns a JsonResult.
I manage to pass the data back to the ajax call but the data I get back is not parsed as a json object but just a string.
My class:
public class CategoryDataViewModel
{
public string name { get; set; }
public List<int> data { get; set; }
public double pointStart { get; set; }
public int pointInterval { get { return 86400000; } }
}
My ajax function called by the highcharts:
function getBugs(mileId) {
var results;
$.ajax({
type: 'GET',
url: 'Services/WebService.svc/GetData',
contentType: "application/json; charset=utf-8",
async: false,
data: { "mileId": parseInt(mileId) },
dataType: "json",
success: function (data) {
console.log(data);
results = data;
}
});
return results;
}
And finally my WebService
public class WebService : IWebService
{
public string GetData(int mileId)
{
string url = "http://localhost:63418/Home/GetWoWData?mileId=" + mileId;
WebRequest wr = WebRequest.Create(url);
using (var response= (HttpWebResponse)wr.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
var objText = reader.ReadToEnd();
return objText;
}
}
}
}
With this when I console.log(data) on the ajax call I get:
[{\"name\":\"Sedan\",\"data\":[30,30,30,30,35],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Low\",\"data\":[800,800,800,826,1694],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Medium\",\"data\":[180,180,180,186,317],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"High\",\"data\":[29,29,29,34,73],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Truck\",\"data\":[6,6,6,6,13],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"SUV\",\"data\":[-172,-172,-172,-179,-239],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Convertible\",\"data\":[0,0,0,0,-404],\"pointStart\":1307836800000,\"pointInterval\":86400000},{\"name\":\"Limo\",\"data\":[-7,-7,-7,-8,-214],\"pointStart\":1307836800000,\"pointInterval\":86400000}]
I can't seem to manage to pass back into a proper Json object. I tried converting it back to my CategoryDataViewModel with this in my webservice:
var myojb = new CategoryDataViewModel ();
using (var response = (HttpWebResponse)wr.GetResponse())
{
using (var reader = new StreamReader(response .GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
myojb = (CategoryDataViewModel )js.Deserialize(objText, typeof(CategoryDataViewModel ));
}
}
return myojb;
But then I get Type 'Test.CategoryDataViewModel' is not supported for deserialization of an array.
Change:
myojb = (CategoryDataViewModel )js.Deserialize(objText, typeof(CategoryDataViewModel ));
to:
myojb = (List<CategoryDataViewModel> )js.Deserialize(objText, typeof(List<CategoryDataViewModel>));
and you should be fine. The array will de-serialize to a list no problem.
I've seen a similar thing before, I think you might need to change myObj to a list.
List<CategoryDataViewModel> myObjs = new List<CategoryDataViewModel>();
...
myObjs = js.Deserialize<List<CategoryDataViewModel>>(objText);
I have the following ASMX web service:
// removed for brevity //
namespace AtomicService
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Assets : WebService
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Validation));
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string CityCreate(string cityname, string state, string country,
decimal timezoneoffset, decimal lat, decimal lon)
{
var response = new Dictionary<string, string>();
if(string.IsNullOrEmpty(cityname) || (string.IsNullOrEmpty(country)))
{
response.Add("Response", "empty");
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
int tzid;
int ctyid;
try
{
tzid = AtomicCore.TimezoneObject.GetTimezoneIdByGMTOffset(timezoneoffset);
var cty = AtomicCore.CountryObject.GetCountry(country);
ctyid = cty.CountryId;
}
catch (Exception)
{
response.Add("Response", "errordb");
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
if(AtomicCore.Validation.DoesCityAlreadyExistByLatLon(cityname, country, lat, lon))
{
response.Add("Response", "exists");
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
const string pattern = #"^(?<lat>(-?(90|(\d|[1-8]\d)(\.\d{1,6}){0,1})))\,{1}(?<long>(-?(180|(\d|\d\d|1[0-7]\d)(\.\d{1,6}){0,1})))$";
var check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
bool valid = check.IsMatch(lat + "," + lon);
if(valid == false)
{
response.Add("Response", "badlatlon");
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
BasicConfigurator.Configure();
Log.Info("User created city; name:" + cityname + ", state:" + state + ", countryid:" + ctyid + ", timezoneid:" + tzid + ", lat:" + lat + ", lon:" + lon + "");
//will return id of created city or 0.
var result = AtomicCore.CityObject.CreateCity(cityname, state, ctyid, tzid, lat.ToString(), lon.ToString(), string.Empty);
response.Add("Response", result > 0 ? "True" : "errordb");
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
}
}
This is called by a JQuery $.ajax call:
$.ajax({
type: "POST",
url: "http://<%=Atomic.UI.Helpers.CurrentServer()%>/AtomicService/Assets.asmx/CityCreate",
data: "{'cityname':'" + $('#<%=litCity.ClientID%>').val() + "','state':'" + $('#<%=litState.ClientID%>').val() + "','country':'<%=Session["BusinessCountry"]%>','timezoneoffset':'" + $('#<%=litTimezone.ClientID%>').val() + "','lat':'" + $('#<%=litLat.ClientID%>').val() + "','lon':'" + $('#<%=litLng.ClientID%>').val() + "'}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
if (msg["d"].length > 0) {
var data = $.parseJSON(msg.d);
if (data.Response > 0) {
//everything has been saved, ideally we
//want to get the cityid and pass it back
//to the map page so we can select it...
alert('Hazzah!');
$(this).dialog('close');
} else {
if(data.Response == 'errordb')
{
alert("db");
}
if(data.Response == 'exists')
{
alert("exists");
}
if(data.Response == 'badlatlon')
{
alert("badlatlon");
}
if(data.Response == 'empty')
{
alert("empty");
}
$('#serviceloader').hide();
$('#<%=txtFindMyCity.ClientID%>:input').attr('disabled', false);
$('#erroroccured').show('slow');
$('#errortext').html("Unfortunately, we can't save this right now. We think this city may already exist within Atomic. Could you please check carefully and try again? If this is an obvious error, please contact us and we'll get you started.");
}
} else {
$('#serviceloader').hide();
$('#<%=txtFindMyCity.ClientID%>:input').attr('disabled', false);
$('#erroroccured').show('slow');
$('#errortext').html("Unfortunately, we can't save this right now. Our data service is not responding. Could you perhaps try again in a few minutes? We're very sorry. Please contact us if this continues to happen.");
}
},
error: function (msg) {
$('#serviceloader').hide();
$('#<%=txtFindMyCity.ClientID%>:input').attr('disabled', false);
$('#erroroccured').show('slow');
$('#errortext').html("Unfortunately, we can't save this right now. Perhaps something has gone wrong with some of our data services. Why not try again? If the problem persists, please let us know and we'll get you started.");
}
});
And despite this, I always receive { "Response" : "False" }. I think this may be a cache problem - but I simply can't get the AJAX to run the service. When I go directly to asset.asmx and invoke the service, the CreateCity method runs correctly.
This is a classic wood for the trees - I have looked at it too long and I'm giving up the will to live...
Thus, the question is: I'd like the CreateCity service work correctly, when called by $.ajax, and not receive a { "Response" : "False" } response. Can anyone provide any direction, help or assistance on how to achieve this with the code I have provided? Please bear in mind that I believe that my service may be caching (hence the { "Response" : "False" } response)...
Help, advice, flames and general comments welcomed...
You don't need to manually JSON serialize the response. This is automatically handled by script enabled services. Simply return a strongly typed object from your web method. Same for the input. Also you must ensure to properly URL encode the request parameters. So start by defining models:
public class City
{
public string Cityname { get; set; }
public string State { get; set; }
public string Country { get; set; }
public decimal Timezoneoffset { get; set; }
public decimal Lat { get; set; }
public decimal Lon { get; set; }
}
public class Response
{
public string Message { get; set; }
public bool IsSuccess { get; set; }
}
and then:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Assets : WebService
{
private static readonly ILog Log = LogManager.GetLogger(typeof(Validation));
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Response CityCreate(City city)
{
var response = new Response();
// ... process and populate the return message,
// set the IsSuccess boolean property which will
// be used by the client (see next...)
return response;
}
}
and on the client side:
$.ajax({
type: 'POST',
url: '<%= ResolveUrl("~/Assets.asmx/CityCreate") %>',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
cityname: $('#<%=litCity.ClientID%>').val(),
state: $('#<%=litState.ClientID%>').val(),
country: '<%=Session["BusinessCountry"]%>',
timezoneoffset: $('#<%=litTimezone.ClientID%>').val(),
lat: $('#<%=litLat.ClientID%>').val(),
lon: $('#<%=litLng.ClientID%>').val()
}),
success: function (result) {
// result.d will represent the response so you could:
if (result.d.IsSuccess) {
...
} else {
...
}
};
});
You can add cache:false to your %.ajax call to make sure it isn't being cached. Have you popped a breakpoint on the service to double check what's being passed through from your jQuery?