package com.json.ss;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonReader {
public static void main(String[] args) {
/*String magnetic_x = "";
String magnetic_y = "";
String magnetic_z = "";*/
String magnetic_xyz = "";
DecimalFormat df = new DecimalFormat("0.000");
String sets = ReadFile("d:/a.json");// 获得json文件的内容
JSONObject jo = JSONObject.fromObject(sets);// 格式化成json对象
System.out.println("------------\n" + jo);
System.out.println("\n");
JSONArray jary = jo.getJSONArray("datas");
System.out.println("------------\n" + jary);
for (int i = 0; i < jary.size(); i++) {
JSONObject obj = jary.getJSONObject(i);
// String magnatic=obj.getString("magnetic");
JSONArray magnetic = obj.getJSONArray("magnetic");
System.out.println("------------\n" + magnetic);
System.out.println("\n");
double z_total = 0.0;
double y_total = 0.0;
double x_total = 0.0;
for (int j = 0; j < magnetic.size(); j++) {
JSONObject obj1 = magnetic.getJSONObject(j);
double z = obj1.getDouble("z");
double y = obj1.getDouble("y");
double x = obj1.getDouble("x");
z_total += z;
y_total += y;
x_total += x;
}
double average_x = x_total / magnetic.size();
// magnetic_x = magnetic_x + df.format(average_x) +"\n";
double average_y = y_total / magnetic.size();
// magnetic_y = magnetic_y + df.format(average_y) +"\n";
double average_z = z_total / magnetic.size();
// magnetic_z = magnetic_z + df.format(average_z) +"\n";
double average_xyz = Math.sqrt(average_x * average_x+average_y * average_y+average_z * average_z);
magnetic_xyz = magnetic_xyz + df.format(average_x) + " " + df.format(average_y) + " "
+ df.format(average_z) +" "+"-"+df.format(average_xyz)+ "\n";
System.out.println("第" + (i + 1) + "个点的" + "x平均值为:" + average_x
+ " " + "y平均值为:" + average_y + " " + "z平均值为:" + average_z
+ "\n");
}
/*
* System.out.println("---x---\n"+magnetic_x);
* System.out.println("---y---\n"+magnetic_y);
* System.out.println("---z---\n"+magnetic_z);
*/
System.out.println(magnetic_xyz);
String fileName = "long";
try {
writeToFile(fileName,magnetic_xyz);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void writeToFile(String fileName, String result)
throws IOException {
String filePath = "D:\\" + fileName+".txt";
File file = new File(filePath);
if (!file.isFile()) {
file.createNewFile();
DataOutputStream out = new DataOutputStream(new FileOutputStream(
file));
out.writeBytes(result);
}
}
// 读文件,返回字符串
public static String ReadFile(String path) {
File file = new File(path);
BufferedReader reader = null;
String laststr = "";
try {
// System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
laststr = laststr + tempString;
++line;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;
}
}
【Android实战】json文件读取并将数据写入文件
最新推荐文章于 2022-02-25 14:41:50 发布