对JSON文件执行操作并将新数组保存到原始JSON文件。 [英] Performing operations on JSON file and saving the new arrays to the original JSON file.

查看:82
本文介绍了对JSON文件执行操作并将新数组保存到原始JSON文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在此处找到JSON文件: drydo.com/crawler/files/json/data.json



滚动到JSON文件的底部以查找数据结构。返回文件并查找位置以查找列标签





这是我到目前为止所做的代码。我开始用Javascript编写,虽然我意识到这是一个错误,但至少现在它终于找到了我为什么它被称为客户端语言。但是不要因此而被绞死,它已经充满了语法错误和缺失的部分。

代码至少包含用于PHP或Python等其他语言的构建块。





 $。getJSON(  Infectious Disease_Cases_by_County_Year_and_Sex_2001_2014.json function (json){


for var i = 0 ; i< json.data.length; i ++){

var data = json.data;


/ * 使用的陈述* /
var 疾病=数据[i] [ 8 ]; / * IF疾病==数据[ - i] [8] * /
var county = data [ i] [ 9 ]; / * IF县==数据[ - i] [9] * /
var year = data [ i] [ 10 ]; / * 如果2001 =< year< = 2014 |可能不需要* /
var gender = data [i] [ 11 ]; / * IFgender ==Total* /
var infectionCase = data [i] [ 12 ];
var population = data [i] [ 13 ];

/ * 仅在最后一个疾病和县与当前[i]相同时执行,必须包含,因为首先发布数据[-1] [x]将为空* /
如果(疾病==数据[ --i] [ 8 ] ||疾病== && county == data [ - i] [ 9 ] ||疾病== && gender = 总计){


foreach(){
var sumInfectionCases;
var sumPopulations;

var sumInfectionCases = sumInfectionCases + infectioncase;
var sumPopulations = sumPopulations + population;
}

}
其他(){

var CountyAverage.byDisease.everyYear = {疾病:数据[ - i] [ 8 ],县:数据[ - i] [ 9 ],平均值:sumInfectionCases / sumInfectionCases};

/ * 将CountyAverage.byDisease.everyYear写入dataManipulation.json * /

}

};


});





需要做的事情是。





1.将疾病和县的所有感染病例总结为每年,2001-2014除以总和每年的总人口。

为每种疾病导致新的JSON数组县平均值(2001-2014)。



2.总结所有县平均值 (2001-2014)每种疾病除以县平均数(2001-2014)

导致每种疾病的新JSON数组全国平均值(2001-2014) 。



3.然后将每个县平均值(2001-2014)除以全国平均值(2001-2014)。

为每个县产生一个新的JSON数组效果大小。







4.将新数组写入meta:部分中的原始JSON文件。

解决方案

.getJSON( Infectious Disease_Cases_by_County_Year_and_Sex_2001_2014.json function (json){


for var i = 0 ; i< json.data.length; i ++){

var data = json.data;


/ * 使用的陈述* /
var 疾病=数据[i] [ 8 ]; / * IF疾病==数据[ - i] [8] * /
var county = data [i] [ 9 ]; / * IF县==数据[ - i] [9] * /
< span class =code-keyword> var year = data [i] [ 10 ]; / * 如果2001 =< year< = 2014 |可能不需要* /
var gender = data [i] [ 11 ]; / * IFgender ==Total* /
var infectionCase = data [i] [ 12 ];
var population = data [i] [ 13 ];

/ * 仅在最后一个疾病和县与当前[i]相同时执行,必须包含,因为首先发布数据[-1] [x]将为空* /
如果(疾病==数据[ --i] [ 8 ] ||疾病== && county == data [ - i] [ 9 ] ||疾病== && gender = 总计){


foreach(){
var sumInfectionCases;
var sumPopulations;

var sumInfectionCases = sumInfectionCases + infectioncase;
var sumPopulations = sumPopulations + population;
}

}
其他(){

var CountyAverage.byDisease.everyYear = {疾病:数据[ - i] [ 8 ],县:数据[ - i] [ 9 ],平均值:sumInfectionCases / sumInfectionCases};

/ * 将CountyAverage.byDisease.everyYear写入dataManipulation.json * /

}

};


});





需要做的事情是。





1.将疾病和县的所有感染病例总结为每年,2001-2014除以总和每年的总人口。

为每种疾病导致新的JSON数组县平均值(2001-2014)。



2.总结所有县平均值 (2001-2014)每种疾病除以县平均数(2001-2014)

导致每种疾病的新JSON数组全国平均值(2001-2014) 。



3.然后将每个县平均值(2001-2014)除以全国平均值(2001-2014)。

为每个县产生一个新的JSON数组效果大小。







4.将新数组写入meta:部分中的原始JSON文件。


没有什么可以阻止你在开始时遍历数据 - 除了我认为你会发现得到你的统计数据很复杂



你没有表明你正在使用的'语言' - 你的片段似乎正在使用? ASP.Net或JavaScript或者?



我采取的方法是(如果我在c#中这样做)



a)使用json2csharp并从你的JSON数据派生一个对象模型

b)将你的json反序列化到你的对象模型

c)对对象模型进行统计操作 - linq等

d)重命名旧文件,使其不再被处理,然后写一个新文件



其可测试,可维护,可审计

You find the JSON-file here: drydo.com/crawler/files/json/data.json

Scroll to the bottom of the JSON-file to find the data structure. Go back up in the file and look for position to find the column labels.


This is the code I've done so far. I began to write in Javascript, although I realize that it was a mistake, but atleast now it finally got to me why it's called "client-side language". But don't get hanged up on that, it's already full of syntax errors and missing pieces.
The code at least contains building blocks to use in another language like PHP or Python.


$.getJSON("Infectious Disease_Cases_by_County_Year_and_Sex_2001_2014.json", function(json) {
  
  
	   for(var i = 0; i < json.data.length; i++) {
	    
	    var data = json.data;
	    
	    
	                               /* Statements to use */
	    var disease = data[i][8]; /* IF disease == data[--i][8] */
	    var county = data[i][9]; /* IF county == data[--i][9] */
	    var year = data[i][10]; /* IF "2001 =< year <= 2014 | MAY NOT BE NEEDED */
	    var gender = data[i][11]; /* IF "gender == "Total" */
	    var infectionCase = data[i][12];
	    var population = data[i][13];
	   
	    /* only executes if last disease AND county was the same as current [i], have to include "" because first post data[-1][x] will be null */
	    if(disease == data[--i][8] || disease == "" && county == data[--i][9] || disease == "" && gender = "Total"){
	    	
	    	
	    	foreach(){
	    	var sumInfectionCases;
	    	var sumPopulations;
	    	
	    	var sumInfectionCases = sumInfectionCases + infectioncase;
	    	var sumPopulations = sumPopulations + population;
	    	}
	    	
	    }
	    else(){
	    	
	    	var CountyAverage.byDisease.everyYear = {disease: data[--i][8], county: data[--i][9], average: sumInfectionCases / sumInfectionCases};
	    	
	    	/* write CountyAverage.byDisease.everyYear to dataManipulation.json */
	    	
	    }
	    
	};
	
  
});



The things that needs to be done is.


1. Sum all "Infection cases" by "Disease" and "County" for "Every year, 2001-2014" divide by the Sum of total population for every year.
RESULTING in a new JSON array "County average(2001-2014)" for each disease.

2. Sum all "County average(2001-2014)" for each disease divide it by the number of "County average(2001-2014)"
RESULTING in a new JSON array "National average(2001-2014)" for each disease.

3. Then divide each "County average(2001-2014)" with the "National average(2001-2014)".
RESULTING in a new JSON array "Effect size" for every county.



4. Write the new arrays to the original JSON-file in the "meta:" section.

解决方案

.getJSON("Infectious Disease_Cases_by_County_Year_and_Sex_2001_2014.json", function(json) { for(var i = 0; i < json.data.length; i++) { var data = json.data; /* Statements to use */ var disease = data[i][8]; /* IF disease == data[--i][8] */ var county = data[i][9]; /* IF county == data[--i][9] */ var year = data[i][10]; /* IF "2001 =< year <= 2014 | MAY NOT BE NEEDED */ var gender = data[i][11]; /* IF "gender == "Total" */ var infectionCase = data[i][12]; var population = data[i][13]; /* only executes if last disease AND county was the same as current [i], have to include "" because first post data[-1][x] will be null */ if(disease == data[--i][8] || disease == "" && county == data[--i][9] || disease == "" && gender = "Total"){ foreach(){ var sumInfectionCases; var sumPopulations; var sumInfectionCases = sumInfectionCases + infectioncase; var sumPopulations = sumPopulations + population; } } else(){ var CountyAverage.byDisease.everyYear = {disease: data[--i][8], county: data[--i][9], average: sumInfectionCases / sumInfectionCases}; /* write CountyAverage.byDisease.everyYear to dataManipulation.json */ } }; });



The things that needs to be done is.


1. Sum all "Infection cases" by "Disease" and "County" for "Every year, 2001-2014" divide by the Sum of total population for every year.
RESULTING in a new JSON array "County average(2001-2014)" for each disease.

2. Sum all "County average(2001-2014)" for each disease divide it by the number of "County average(2001-2014)"
RESULTING in a new JSON array "National average(2001-2014)" for each disease.

3. Then divide each "County average(2001-2014)" with the "National average(2001-2014)".
RESULTING in a new JSON array "Effect size" for every county.



4. Write the new arrays to the original JSON-file in the "meta:" section.


There's nothing to stop you traversing your data as you have started off doing - except I think you'll find it complex to derive your stats

You dont indicate a 'language' you are using - it seems from your fragment you're using ? ASP.Net or JavaScript or ?

The approach I would take is (if I were doing it in c#)

a) use json2csharp and derive an object model from your JSON data
b) deserialise your json to your object model
c) do your stats operations on the object model - linq etc
d) rename the old file so it wont get processed again, then write a new file

its testable, maintainable, auditable


这篇关于对JSON文件执行操作并将新数组保存到原始JSON文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆