[英]Json data reading in client side in asp.net application
I have the following data as json: 我有以下数据作为json:
"Table":[
{
"AF":2000.00
"RegionNumber":1
"RegionName":"Black Sea"
},
{
"AF":100.00
"RegionNumber":1
"RegionName":"Black Sea"
},
{
"AF":15000.00
"RegionNumber":2
"RegionName":"Istanbul"
},
{
"AF":31000.00
"RegionNumber":1
"RegionName":"Black Sea"
},
{
"AF":11000.00
"RegionNumber":2
"RegionName":"Istanbul"
}
]
I want to arrange the data in the following format in Javascript. 我想以以下格式在Javascript中排列数据。
series: [{
name: 'Black Sea',
data: [2000, 100, 31000],
stack: 'Bookings'
}, {
name: 'Istanbul',
data: [15000,11000,0],
stack: 'Bookings'
}]
How can I accomplish this transformation? 我怎样才能完成这一转变?
This almost does what you ask and uses Ramda.js too 这几乎可以满足您的要求,也可以使用Ramda.js
const data = { Table: [ { AF: 2000, RegionName: "Black Sea", RegionNumber: 1 }, { AF: 100, RegionName: "Black Sea", RegionNumber: 1 }, { AF: 15000, RegionName: "Istanbul", RegionNumber: 2 }, { AF: 31000, RegionName: "Black Sea", RegionNumber: 1 }, { AF: 11000, RegionName: "Istanbul", RegionNumber: 2 } ] } const transfromTableData = R.pipe( R.groupBy(R.prop("RegionName")), R.map(R.pluck("AF")), R.toPairs, R.map(R.zipObj(["name", "data"])) ) const transfromData = (data) => ({ series: transfromTableData(data.Table) }) console.log(transfromData(data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
In pure JavaScript: 在纯JavaScript中:
var memo = {}
for (var i in table) {
var item = table[i];
if (memo[item.RegionName] !== undefined) memo[item.RegionName].data.push(item.AF);
else memo[item.RegionName] = {
name: item.RegionName,
data: [item.AF],
stack: 'Bookings'
}
}
var result = {
series: []
};
for (var i in memo) {
result.series.push(memo[i]);
}
console.log(result);
I'm passing arrays of custom objects into List in web methods and it works just fine. 我正在Web方法中将自定义对象的数组传递到List中,它工作得很好。
I'm, guessing that you're having a small JSON formatting issue because of the quotes around the property names. 我正在猜测,由于属性名称周围的引号,您遇到了一个小的JSON格式问题。 Try changing your object to this :
尝试将您的对象更改为此:
var scoresList = [{TraitID:1, TraitScore:2}, {TraitID:2, TraitScore:5}];
and change your data line to this : 并将您的数据行更改为:
data: JSON.stringify({ scores : scoresList }),
Hope that helps... 希望有帮助...
UPDATE: working example... 更新:工作示例...
<script type="text/javascript">
$(function () {
var scoresList = [{ TraitID: 1, TraitScore: 2 }, { TraitID: 2, TraitScore: 5}];
$.ajax({ type: "POST",
url: "Tryouts.aspx/Test",
data: JSON.stringify({ scores: scoresList }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d == true) {
alert("success!!!!!");
} else {
alert("problem!!!!!!!!!");
}
},
error: function (xhr) {
alert("ERROR");
}
});
});
</script>
Here's the codebehind : 这是背后的代码:
public class Score
{ // default constructor
public Score() { }
public int TraitID { get; set; }
public double TraitScore { get; set; }
}
[WebMethod]
public static bool Test( List<Score> scores )
{
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.