Working with identifiers as arrays and sending to MVC controller - javascript

I have some identifiers on my page. Named id[0], id[1] all the way to id[20] and sometimes more. I need to send these to an MVC controller and I would like to package them up as one object and unpack at the controller. Can someone tell me if this is possible, my knowledge of javascript is just basic so I'd really appreciate advice on which way to go. For example can I use JSON or serialize. btw I'm using jQuery.
Gordon

You can send your data in JSON.
See > ASP.NET MVC How to pass JSON object from View to Controller as Parameter

If you already have the id array (id[0], id1 , ... id[20]) you can convert it to JSON string with respect of JSON.stringify(id) where JSON.stringify function defined in the json2.js.
On the server side you can use for example Deserialize method of the JavaScriptSerializer to convert the data to List<T> where T is type of id[i] (for example string).
How to send data with respect of jQuery.ajax you already know from another answer.

Related

how to convert breeze entity to json

I know this question has been asked before and there is a very old post with some details however it is dated and there was some discussion to add this to breeze.
I am using angularJS and a telerik component which doesn't work with breeze entities, i would very much like to convert a breeze entity query list to simple json so that i can map it to my component.
I know there has been mention of ko.toJs() but i do not use knockout. Is there a better method to do this?
Thanks
Use the noTracking EntityQuery method:
From the breeze API docs:
noTracking()
Returns a query with the 'noTracking' capability either enabled or disabled. With 'noTracking' enabled, the results of this query will not be coerced into entities but will instead look like raw javascript projections. i.e. simple javascript objects.
var query = new EntityQuery("Customers")
.take(20)
.orderBy("CompanyName")
.noTracking(true);

Get array from separate PHP file and store into jQuery array

I've searched around and wasn't able to find a clear path for accomplishing this.
I'm familiar with using json_encode() and json_decode() for converting php arrays to and from json, however I'm stumped about the below scenario:
I have a user interactive page where they need to access and use data from one of dozens of php array files. I have a php file (get-array.php) that determines which array to send back based on what the user has been selecting.
Normally I would just use .load() to load php results into a div, but I need the array back and stored in a jQuery array. What would be the best method for doing this?
Many thanks!
Use http://api.jquery.com/jQuery.ajax/ or the shorthand method, http://api.jquery.com/jQuery.get/ to retrieve JSON.
Or even better, this shorthand method is made just for this: http://api.jquery.com/jQuery.getJSON/
When you have your PHP array a d are ready to send it, you can use the JSON encode method of PHP to prepare it as JSON: http://php.net/manual/en/function.json-encode.php

JSON to String on client side for ASP.NET Script Service?

From this site, I've learned that ASP.NET script services accepting JSON actually require them to be serialized JSON strings (see: "JSON, objects, and strings: oh my!" section of the link). Is there a quick and easy way to serialize them for ASP.NET AJAX consumption on the client side instead of trying to manually convert a bunch of existing objects to JSON-looking strings?
Thanks in advance!
You can use JSON.stringify() to serialize client-side objects for consumption in ASP.NET's Script Services.
Using that approach, you can map client-side objects to server-side objects very easily. ASP.NET will automatically handle converting the JSON to objects (or even collections of objects) for you.
The article writer is confusing Javascript objects with JSON strings. There is no such thing as a "JSON object".
Naturally if you try to send an object to a web service, it has to be serialised, as the request data can only contain text, not objects. The standard way of serialising data to be posted is URL encoding it, so that is what jQuery does.
There is no JSON serialisation built into Javascript or jQuery. You would have to do the serialising yourself or find a library that does it. Here are some options: Serializing to JSON in jQuery
Also, the data sent in the example is not valid JSON. It looks like this:
"{'fname':'dave', 'lname':'ward'}"
To be valid JSON it should look like this:
'{"fname":"dave", "lname":"ward"}'

Traverse array list in Javascript file

In my Struts 2 based application, I have an ArrayList. How can I send this ArrayList to a Javascript file so that I can compare list item with element of jsp and show some message based on validation? How can I use the array list data in Javascript file.
I do not want use JSON.
JSON comes to mind - not sure of this example so here is one more
you can use <logic:iterate> and iterate through your List, here is example
Your question makes it seem like you want to send the ArrayList to the client directly, this means it will not be processed by a JSP. The easiest way to do this is with JSON, then XML, then perhaps some custom built format which you'd need to parse with JS.
But since you then say that you want to compare a value from the action class to a value in the JSP this means you'll want the comparison done when composing the view on the server and not on the client. For this see the struts2 tag documentation the <s:iterate> and <s:if> tags will let you do this.
As your question stands the two ideas are irreconcilable without some guess work.

reading a json object in jsp

i have a JSON object passed to the jsp page. it is passed as a string. now i have to parse this string and retrieve the values that are passed through the JSON object. so that i can print the values in the same jsp.
There's lots of resources, including libraries & plugins for various technologies/frameworks on json.org.
With tons of JSON parsers, it comes down to how you want to deal with data in JSON. My personal favorite from the lot is Jackson, but many others work well for simple cases too, including the "reference implementation" (aka JSON.org parser).
(I assume you want a Java parser, given reference to jsp)
My preferred solution to this problem involves using a JSON parser that provides an output that implements the java.util.Map and java.util.List interface. This allows for simple parsing of the JSON structure in the JSP Expression language.
Here is an example using JSON4J provided with Apache Wink. The sample imports JSON data from a URL, parses it in a java scriptlet and browses the resulting structure.
<c:import var="dataJson" url="http://localhost/request.json"/>
<%
String json = (String)pageContext.getAttribute("dataJson");
pageContext.setAttribute("parsedJSON", org.apache.commons.json.JSON.parse(json));
%>
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}
To make this clean, it would be preferable to create a JSTL tag to do the parsing and avoid java scriplet.
<c:import var="dataJson" url="http://localhost/request.json"/>
<json:parse json="${dataJson}" var="parsedJSON" />
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}

Categories

Resources