How to convert JS array to JSON, before passing that to controller (using AJAX - POST call) - asp.net-mvc

I ve a JS array, comprising multiple JS objects.
I want to convert JS array to JSON type, & pass that to controller (using AJAX - POST call).
So that I can retrieve the values from the Array of Objects, in my controller & save them in DB.
NB: I ve tried using $.stringify(myArry), but its not able to send data to controller in JSON format. Also I cant use $.toJSON(myArray), as I m not allowed to include a new plugin in our solution. :(
Plz suggest me any other idea.
Else if anyone can let me know how to deserelize the array in cotroller, that I ve sent by using $.stringify(myArry), that would also great.
Something like
var result = JavaScriptConvert.DeserializeObject(inputContent, JsonDataType);

Per this post, it looks like you'll have to add another plug-in:
JSON stringify missing from jQuery 1.4.1?
The Google solution looks good: http://code.google.com/p/jquery-json/
Or just use the JSON object from Crockford: https://github.com/douglascrockford/JSON-js

Related

Maximo Integration: Putting sub-objects in a response to SOAP

Need help! I'm having a problem with putting sub-objects in a response from Maximo to SOAP.
for Example, I want my response to look like this:
<mainObject>
<--Imagine mainObject fields here-->
<subObject>
<--Imagine subObject fields here-->
</subObject>
</mainObject>
I can't seem to find any sample from the internet that I can use as a reference.
Can anyone give me some clues/hints?
You can use the method createChildrenData() to create a new sub-object and then add the fields to that. For example:
irData.createChildrenData(subObject)
I had to decompile the StructureObject class to get that method. You'll need to create a new WSDL for this modified response, otherwise the end point won't recognize the new format.

How to send multiple array in JSON in asp.net mvc?

I have a 3 <tr></tr> in my table. user can edit them and on the save button I send the data to the server.
first is main and other all is child of the main. When someone click on new button a new child is created.
now I am thinking to maintain the information like this
var minf = {};
minf.main = $("#tr" + curSplidId).find('input,select').serialize();
res.each(function(n) {
var i = $(this).find('input,select').serialize();
minf[n] = i;
});
All I am trying to do is getting main object and array of childs in JsonResult, I have tried to use Dictionary for JSON.stringify values.
None of these works.
Someone please help me to get it done. Through my testing I found in a case it's sending me querystring in my minf object (I does Stringify) but I am not sure how to handle it on JSONResult as some kind of dictionary stuff where I can read it through the keys.
suppose you have a 3 tr. the best approach is to use a loop on all the tr and create a object same to the DTO that is using in action and make a list of it.
var MainList=[];
$('tr').each(function(){
var MainDTO={};
MainDTO.Column1=$(this).find('td').html();
MainDTO.Column2=$(this).find('td').html();
MainList.add(MainDTO)
});
and pass this MainList directly to action. no serialise required
I think for converting data to json and read that json on server. I make the whole process hard for JS and server to handle.
I change the strategy to serialize all the element and send it to server. it's work fine.
now if I need to read id then I can parse the id which is 3 and come in comma based values.

Swift 2.0: How to implement tableview controller with JSON API

I think this is a probably repeat question.
I am new to ios and and I am trying to learn so I don't expect response in actual codes. I still want to write them myself.
Here's what I have done so far:
I wrote codes to make web api calls, authenticate and pull json data.
I've figured out how to parse json data that I pulled from web.
I have made tableview controller.
Challenge: How do I get the parsed Json data to tableview controller?
Do I:
- save it locally into like NSUSERDefaults variable?
- save it to a global variable shared by controller and my model?
- at the end of the parsing json data do a return so that it will be saved into viewController?
- or none of the above and do ______________ (insert your answer)
What's the cleanest way to do this? The table will initially have like 20 rows but as you pull down you will get more rows.
Best way to save data on model class while parsing and store this class objects in an array and in cellforrowatindexpath get objects of model class from array and show data using these objects.

How to encode javascript string for display and post back?

I have an MVC application that is rendering rendering the following javascript on the client:
var rawData = [{"ID":5317,"Code":"12345","Description":"sometext \u003c/= 100"}];
The JSON data is a result of serializing an object using the JavaScriptSerializer and then running the result through the Html.Raw() helper.
This data is then used to load a knockout view model and display a popup on hover. In the popup, only the "sometext" portion of the "Description" property is being shown as the string gets converted to the unencoded version when setting the rawData variable (i.e. \u003c is converted to <).
Also, this data ends up being sent back to the server upon saving of data, and the ASP.NET validation kicks in and fails the request as it detects the "
I've worked around this, temporarily, by adding a computed property to my Knockout View Model like so:
self.DescriptionEncoded = ko.observable('');
self.Description = ko.computed({
read: function() {
return self.DescriptionEncoded ();
},
write: function(value) {
self.DescriptionEncoded($('<div/>').text(value).html());
}
});
In this way I can access the escaped property from my popup and the unescaped value is not sent back to the server when I serialize my viewmodel (using .toJSON()).
Is there a more global way to handle this rather than creating computed properties for every object that may have some text that appear to be a bad request while not compromising on security? I've considered an overload/helper to the serialization routine that would accept a list of properties to apply a Find/Replace I am thinking this will have to be handled on a case by case basis in a manner similar to what I've already done. As for sending the data back to the server, I could override the toJSON() method on my view model and delete the properties that don't need to be sent back, but that won't help me with my popup.
Thoughts?
You can encode using Ajax.JavaScriptStringEncode. You might also get the AntiXSS library and use it for the encoding.
I hope I understood your question well.

Translating javascript json parse to rails

I had to change an application that was making a call clientside (JS) to get back data that comes back as JSON. I had to move the call server side, and I'm using rails to return the data.
To reference the object I need, with the object being returned called "data" I was able to call it JS like so:
data.photos[0].tags[0].mouth_left.x
I'm very new to rails, I have the call populating a variable called face_values, I think I should call to_json on it next, but how do I reference the arrays and nested objects within?
Even a point in the right direction would be great, thank you.
parsing JSON in Rails is as follows:
parsed_json = ActiveSupport::JSON.decode(your_json_string)
or check out this link
They claim they are 21.5x faster than ActiveSupport::JSON.decode

Resources