I am a novice trying to deserialize my result from an onSuccess function as :
"onResultHttpService": function (result, properties) {
var json_str = Sys.Serialization.JavaScriptSerializer.deserialize(result);
var data = [];
var categoryField = properties.PodAttributes.categoryField;
var valueField = properties.PodAttributes.valueField;
for (var i in json_str) {
var serie = new Array(json_str[i] + '.' + categoryField, json_str[i] + '.' + valueField);
data.push(serie);
}
The JSON in result looks like this:
[
{
"Text": "INDIRECT GOODS AND SERVICES",
"Spend": 577946097.51
},
{
"Text": "LOGISTICS",
"Spend": 242563225.05
}
]
As you can see i am appending the string in for loop..The reason i am doing is because the property names keep on changing therefore i cannot just write it as
var serie = new Array(json_str[i].propName, json_str[i].propValue);
I need to pass the data (array type) to bind a highchart columnchart. But the when i check the var serie it shows as
serie[0] = [object Object].Text
serie[1] = [object Object].Spend
Why do i not get the actual content getting populated inside the array?
You’re getting that because
json_str[i]is an object, and that’s what happens when you coerce an object into a string (unless the object implementstoStringin a useful way, which this one clearly doesn’t).You haven’t shown the JSON you’re deserializing…Now that you’ve posted the JSON, we can see that it’s an array containing two objects, each of which has a
TextandSpendproperty. So in your loop,json_str[i].Textwill refer to theTextproperty. If you want to retrieve that property using the name incategoryField, you can do that viajson_str[i][categoryField].I don’t know what you want to end up with in
serie, but if you want it to be a two-slot array where the first contains the value of the category field and the second contains the value of the spend field, then(There’s almost never a reason to use
new Array, just use array literals —[...]— instead.)