I am looping through a JSON table using jquerys foreach operator. I am having to reference the the columns by name e.g:
$.each(items, function (index, item) { if (item.CustomerFname == "Bob") { alert(item.CustomerFname + " " item.CustomerSname) }
Whereas I would like to be able to say something like:
[code]....
Ultimately I'm trying to make this function dynamic with respects to the json list I pass in which will of course mean that the column names will change. However I would like to perform the same operations on the columns...
I have a list of person objects which I want to send in response to a jquery' ajax request. I want to send the list into JSON format. The list is as follows -
List<Person> PersonList = new List<Person>(); Person Atiq = new Person("Atiq Hasan Mollah", 23, "Whassap Homie"); Person Sajib = new Person("Sajib Mamud", 24, "Chol kheye ashi"); PersonList.Add(Atiq); PersonList.Add(Sajib);
How can I convert the "PersonList" list into JSON ?
I'm new to Jquery, JSON and MVC, and am trying to create a json object that contains AD info from an AD query. The ad query is working fine and returning data and I have 2 overloaded implementations of it.
#1 is a method from an older .net app that builds and returns a datatable that we bound to a grid view control
#2 was a modification of #1 that returns a System.Collections.Generic.List<> type. #2 returns data ok and i can display a table in the MVC view
however, I'd like to get the data results into a json object so i can bind it to a jqGrid object that has similar functionality to the old .Net server controls (Delete, Update, Sort) Here's the new method that's part of a C# static class:
[Code]....
Like I said...I'm new to this way of doing web development, so I'm not sure how to even approach getting this data into a json object.
I have dropdown in the aspx page. I am calling JQuery on dropdown chnage and JQuery is calling GenericHandler.ashx . The Handler is retuns Serialize JSON string. I ned to bind my Repeater control with this JOSN object. here I am giving my sample aspx page code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">
With in Value another array of objects. So here we know that Key A is found on index 0. To get A value I use if(list[i].Key=='A'){...} . I would like to directly navigate to Key with value A without using a loop first to find it, so I thought of a index mechanism for this.
I am trying to build a page which consists of different user controls which is divided in 3 columns.I have created a custom class for the page and widgets where the user controls will be loaded in. The custom class needs to be stored in an XML file, and using jQuery sortable for drag-and-drop functionality.At this point, my question is what is the best implementation for loading and saving the states of the page and widgets inside the page? I was thinking about using JSON and WebMethod for that, but I am still missing out the architecture on how I should build the page
I have an ASP.NET webservice method that returns a generics list (List'<'Construct>) serialized as JSON, using code such as this:
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class TestService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetOccupationListJSON(int SOCLevel) { Construct NewConstructList = new ConstructList(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(ConstructList.GetType()); MemoryStream ms = new MemoryStream();
[Code]....
I assumed (from looking elsewhere) that accessing the JSON data through the index would provide me with access to the underlying object at that index, so that I can then work with it to access its properties. However, when i=0 and I do var Construct = data[i]; I get the character at the i position of the data array ([), and in the next iteration I get the second character ({). So clearly I am accessing the elements of a string array rather than the JSON data object.How do I make sure that the data returned by the webservice gets into proper JSON format, so that I can iterate through the object elements within it?
I use the JavaScriptSerializer class of ASP.net to serialize my object and return it to the client side. How can I deserialize the string using JavaScript?
I want to send single json object with nested arrays through jquery ajax method.
In Code
In jquery ajax i am passing LeaveRuleMaster a single json object which holds some properties (Leavetypeid, Leavename, Leavestatus and nested array LeaveRulespecific.
In vb.net code i can receive the value of properties(Leavetypeid,Leavename,Leavestatus)
but i am receiving the LeaveRuleSpecific as nothing.
Based on the following link i tried this [URL] .....
I've scenario where I want to insert data into database without post back. there are around 12 to 13 fields which i need to insert. I'm passing DTO from the client side which is actually Json object. Now the problem which i'm facing is how to convert that Json object which i got in webservice to the "class" (in my case class name is User) object.
[Code]....
In the above case AddNewUser method takes the object of User class. But i'm getting casting error. So how do I convert Json object to the "User" class object?
Is there an easy way to populate my C# Object with the JSON object passed via AJAX?
//This is the JSON Object passed to C# WEBMETHOD from the page using JSON.stringify {"user":{"name":"asdf","teamname":"b","email":"c","players":["1","2"]}} //C# WebMetod That receives the JSON Object [WebMethod] public static void SaveTeam(Object user) { } //C# Class that represents the object structure of JSON Object passed in to the WebMethod public class User { public string name { get; set; } public string teamname { get; set; } public string email { get; set; } public Array players { get; set; } }
I have problem with JSON, MapRoute. If I click on "save" I always get Page / Index Page instead of / SendForm. I dont know why, in javascript it says url: '/ page / sendform'. But the problem is at Global.asax.cs,
But there is still problem at the url, which shows Controller, Action that I dont want to .. I have a url / lang / seo-url /. How can I achieve by clicking on the Save, it should call SendForm method instead of Index ..
public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("page", "{lang}/{url}", new { controller = "Page", action = "Index", lang = "cz", url = UrlParameter.Optional }); routes.MapRoute("Default", "{controller}/{action}/{url}", new { controller = "Page", action = "Index", url = UrlParameter.Optional }); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); } } Index.aspx <script type="text/javascript"> $(function () { $("#personCreate").click(function () { var person = getPerson(); // poor man's validation if (person == null) { alert("Specify a name please!"); return; } var json = $.toJSON(person); $.ajax({ url: '/page/sendform', type: 'POST', dataType: 'json', data: json, contentType: 'application/json; charset=utf-8', success: function (data) { // get the result and do some magic with it var message = data.Message; $("#resultMessage").html(message); } }); }); }); function getPerson() { var name = "Ruda"; var age = "18"; // poor man's validation return (name == "") ? null : { Name: name, Age: age }; } </script> <input type="submit" value="Save" id="personCreate" /> PageCollector.cs public class PageController : Controller { // // GET: /Page/ public ActionResult Index(string lang, string url) { switch(url) { case "anglictina": return View("~/Views/English/Index.aspx"); case "nemcina": return View("~/Views/German/Index.aspx"); case "prihlaska": return View("~/Views/Application/Index.aspx"); case "galerie": return View("~/Views/Galerie/Index.aspx"); case "kontakt": return View("~/Views/Contact/Index.aspx"); default: return View("~/Views/English/Index.aspx"); } } [HttpPost] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public JsonResult SendForm(string name, string age) { return Json(string.Format("Message: '{0}'", name)); } }
From VB.net code behind when I am trying to deserialize it it's stating that
"Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''."
Deserialization code snippet:
Dim serializer = New DataContractJsonSerializer(GetType(List(Of Employee))) Dim memoryStream = New MemoryStream() Dim s = msg.Content.ReadAsString() serializer.WriteObject(memoryStream, s) memoryStream.Position = 0.......
I want to serialize an object to json, so I'm using [Code]....
[Code]....
This is fine, but I want to return different json datasets depending on the circumstances. Lets say I have this object (a simplified example):
[DataContract] class foo { [DataMember] int productId [DataMember] string productName double price; //not used in json.. yet }
Which will serialize to a json object containing the product name & Id, but what if I also need to be able to return a different json object (say, productId and price but not name)? Can this be done somehow in the same object using WCF/DataContracts and DataMembers? This will be sent in an ajax query containing a collection of objects and needs to fast, so just including every member in every type of json request whether it's needed or not isn't a good solution.
I have an object called MyObject that has several properties. MyList is a list of MyObject that I populate with a linq query and then I serialize MyList into json. I end up with something like this
List<MyObject> MyList = new List<MyObject>();
MyList = TheLinqQuery(TheParam);
var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer();
string MyJson = TheJson.Serialize(MyList);
What I want to do is serialize only parts of MyObject. For instance, I might have Property1, Property2...Propertyn and I want MyJson to only include Property3, Property5 and Property8. I thought of a way to do this by creating a new object with only the properties I want and from there create a new list for the serialization.
I could use the DataContractJsonSerializer Class and have custom Classes defined. However, am not sure how keys like "$" would get converted to in .Net.