JSON兄弟姐妹数据 [英] json sibling data
问题描述
(原谅我,如果我用稍微不正确的语言 - 随时根据需要建设性正确)
有大约从返回的对象的兄弟姐妹的JSON数据获取数据一对夫妇的职位,但我有将这些信息来我的情况麻烦:
我有一堆都可以从一个REST调用,并用于与某些关键的一个节点的每个对象返回的JSON对象:值,我需要提取特定密钥的兄弟节点的数值。例如:
有关对象的以下列表,我需要添加上的数字,FILE_SIZE为每个对象相匹配的递减,并返回到页面上匹配的输入值。
{结果集:{
结果:[
{
FILE_SIZE:722694,
说明:说明1,
格式化:GIF
},
{
FILE_SIZE:19754932,
说明:说明1,
格式化:JPEG
},
{
FILE_SIZE:778174,
说明:内容描述,
格式化:GIF
},
{
FILE_SIZE:244569996,
说明:说明1,
格式化:PNG
},
{
FILE_SIZE:466918,
说明:内容描述,
格式化:TIFF
}
]
}}
您可以使用下面的功能:
函数findSum(介绍,数组){
变种I = 0;
变种总和= 0;
对于(i = 0; I< array.length;我++){
如果(数组[我] [说明] ==描述&功放;&放大器;数组[我] .hasOwnProperty(FILE_SIZE)){
总和+ = parseInt函数(数组[我] [FILE_SIZE],10);
}
}
警报(总和);
}
和调用它是这样的:
findSum(内容描述,ResultSet.Result);
要与内容描述
文件大小的全部总和,显示一个警告。
一个工作的jsfiddle是在这里: http://jsfiddle.net/Q9n2U/
在回答您的更新和评论,这里有一些新的code,与求和所有的描述产生了一些 DIV
秒。我拿出了的hasOwnProperty
code,因为你改变了你的数据集,但需要注意的是如果您有数据数组中的对象,而不 FILE_SIZE
属性,你必须使用的hasOwnProperty
来检查它。您应该能够调整此为您的jQuery 。每次
相当容易。
VAR数据= {};
VAR阵列= ResultSet.Result;
变种I = 0;
VAR currentDesc,currentSize;
VAR sizeDiv;
VAR sumItem;
//总和大小每个描述
对于(i = 0; I< array.length;我++){
currentDesc =阵列[I] [说明]。
currentSize = parseInt函数(数组[我] [FILE_SIZE],10);
数据[currentDesc] =
typeof运算数据[currentDesc] ===未定义
? currentSize
:数据[currentDesc] + currentSize;
}
//打印求和来的div在页面上
对于(sumItem数据){
如果(data.hasOwnProperty(sumItem)){
sizeDiv = document.createElement方法(分区);
sizeDiv.innerHTML = sumItem +:+数据[sumItem]的ToString();
document.body.appendChild(sizeDiv);
}
}
一个工作的jsfiddle是在这里: http://jsfiddle.net/DxCLu/
(forgive me if I use slightly incorrect language - feel free to constructively correct as needed)
There are a couple posts about getting data from JSON data of siblings in the returned object, but I'm having trouble applying that information to my situation:
I have a bunch of objects that are getting returned as JSON from a REST call and for each object with a node of a certain key:value I need to extract the numeric value of a sibling node of a specific key. For example:
For the following list of objects, I need to add up the numbers in "file_size" for each object with matching "desc" and return that to matching input values on the page.
{"ResultSet":{
Result":[
{
"file_size":"722694",
"desc":"description1",
"format":"GIF"
},
{
"file_size":"19754932",
"desc":"description1",
"format":"JPEG"
},
{
"file_size":"778174",
"desc":"description2",
"format":"GIF"
},
{
"file_size":"244569996",
"desc":"description1",
"format":"PNG"
},
{
"file_size":"466918",
"desc":"description2",
"format":"TIFF"
}
]
}}
You can use the following function:
function findSum(description, array) {
var i = 0;
var sum = 0;
for(i = 0; i < array.length; i++) {
if(array[i]["desc"] == description && array[i].hasOwnProperty("file_size")) {
sum += parseInt(array[i]["file_size"], 10);
}
}
alert(sum);
}
And call it like this:
findSum("description1", ResultSet.Result);
To display an alert with the summation of all "description1"
file sizes.
A working JSFiddle is here: http://jsfiddle.net/Q9n2U/.
In response to your updates and comments, here is some new code that creates some div
s with the summations for all descriptions. I took out the hasOwnProperty
code because you changed your data set, but note that if you have objects in the data array without the file_size
property, you must use hasOwnProperty
to check for it. You should be able to adjust this for your jQuery .each
fairly easily.
var data = {};
var array = ResultSet.Result;
var i = 0;
var currentDesc, currentSize;
var sizeDiv;
var sumItem;
//Sum the sizes for each description
for(i = 0; i < array.length; i++) {
currentDesc = array[i]["desc"];
currentSize = parseInt(array[i]["file_size"], 10);
data[currentDesc] =
typeof data[currentDesc] === "undefined"
? currentSize
: data[currentDesc] + currentSize;
}
//Print the summations to divs on the page
for(sumItem in data) {
if(data.hasOwnProperty(sumItem)) {
sizeDiv = document.createElement("div");
sizeDiv.innerHTML = sumItem + ": " + data[sumItem].toString();
document.body.appendChild(sizeDiv);
}
}
A working JSFiddle is here: http://jsfiddle.net/DxCLu/.
这篇关于JSON兄弟姐妹数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!