javascript - How to iterate over object sent via AJAX JSON -
i have following javascript object:
var parameters = { "parameters" : [ { "key": "feedbacksource", "value": "foo" }, { "key": "status", "value": "foo" }, { "key": "feedbacktype", "value": "foo" } ] };
console.log(json.stringify(parameters)) shows:
{"parameters":[{"key":"feedbacksource","value":"foo"},{"key":"status","value":"foo"},{"key":"feedbacktype","value":"foo"}]}
ajax:
$.ajax({ type: "post", url: "mypage.aspx/mymethod", data: json.stringify(parameters), contenttype: "application/json; charset=utf-8", datatype: "json" });
method:
[webmethod] public static void mymethod(object parameters) { }
question: how iterate on object in c# contents?
i have tried:
foreach (var p in (ienumerable) parameters) { foreach (var x in (ienumerable) p) { var test = x; } }
but test
on first iteration key value pair, key = "key"
, value = "feedbacksource"
. on second iteration, key = "value"
, value = "foo"
and doesn't seem correct way iterate on object. expect key = "feedbacksource"
, value = "foo"
.
"how iterate on object in c# contents"
after discussion @devlin - following conclusion has been reached:
current json data structure
the json string being generated (from page) little confusing least.
the existing data structure of
{ "parameters" : [ { "key": "feedbacksource", "value": "foo" }, { "key": "status", "value": "foo" }, { "key": "feedbacktype", "value": "foo" } ] };
was confusing , obfuscated data - making hard traverse , iterate objects inside.
it appeared of key:(key/value) pairs/pairs (wait, what?)
i proposed structure proper key/value pair structure json firstly (from within page).
like so:
{ "parameters" : [ { "feedbacksource" : "foo" }, { "status" : "foo" }, { "feedbacktype" : "foo" } ] }
what modelbinder doing?
the modelbinder cleverly assiging parameter
dictionary<string, object>
, because recognised json key/value pairs - in values yet again key value pairs.
obviously, every type in c# derives object
, accepting boxing in object
.
thus why other suggestions of casting string
weren't successful.
asp.net has modelbinder able detect types of objects passed methods, make easier when getting data out in method.
passing in/using right type
suggestion 1
cast object parameter
in mymethod
signature dictionary<string, string>
(do safely, possible)
like so:
var paramsdict = parameters dictionary<string, object>; if (paramsdict != null) { // iterate on dictionary in here }
suggestion 2
seeing know object parameters
of type dictionary<string, object>
(thanks wizardry modelbinder), use type in mymethod
signature, instead.
like this:
[webmethod] public static void mymethod(dictionary<string, object> parameters) { // iterate in here }
iterating data
now you've got nice , easy use dictionary<string, object>
, simple iterate on , grab values there.
like this:
foreach (var param in paramsdict) // or "... in parameters" if you're using suggestion 2 { var key = param.key; var value = param.value; // manipulate/persist data }
it's how manipulate/persist data, should give starting point of getting data json object.
summary
- change json structure "proper" key/value pairs
- default modelbinder able assign json c#
dictionary<string, object>
- either change
mymethod
signature acceptdictionary<string, object>
typeparameter
, or castparameter
objectdictionary
(both should work same) - iterate each item in
dictionary
. can accesskey
s ,value
s of each item, , manipulate data wish
furthermore
given json string passing in key/value pairs of string
, string
, suspect may safely use dictionary<string, string>
picked modelbinder, or when casting etc. confirmation may needed on this, however
hope helps, , best of luck in project! :)
Comments
Post a Comment