C# 读取文件中的Json数据进行处理输出
public void JsonMy()
{
var fileName = Server.MapPath("ReadSenseFaceUSBKey") + "\\Rs.json";//Jsonstr函数读取json数据的文本
string json = Jsonstr(fileName);//Jsonstr函数读取json数据的文本txt
JsonDataT result = JsonConvert.DeserializeObject<JsonDataT>(json);
if (result.status == "ok")
{
FaceDataT model = result.data;
string confidence = model.confidence;
int[] intA = model.facerect;
string facerect = "[";
foreach (var a in model.facerect)
{
facerect += a + ",";
}
facerect = facerect.TrimEnd(',')+"]";
string faceQuality = model.faceQuality;
txtResult.Text = confidence + "---" + faceQuality + "----" + facerect;
}else
{
txtResult.Text = "获取特征值错误";
}
}
public string Jsonstr(String filePath)
{
string strData = "";
try
{
string line;
// 创建一个 StreamReader 的实例来读取文件 ,using 语句也能关闭 StreamReader
using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
{
// 从文件读取并显示行,直到文件的末尾
while ((line = sr.ReadLine()) != null)
{
//Console.WriteLine(line);
strData = line;
}
}
}
catch (Exception e)
{
// 向用户显示出错消息
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
return strData;
}
}
//返回对象
public class JsonDataT
{
//数据
public FaceDataT data { set; get; }
//状态
public string status { set; get; }
}
//特征对象
public class FaceDataT
{
public string confidence { set; get; }
//面部特征
public string faceQuality { set; get; }
//面矩形
public int[] facerect { set; get; }
//特征(局部 口、鼻)
public int[] feature { set; get; }
//抛
public string pitch { set; get; }
//翻转
public string roll { set; get; }
//非常宽
public string yaw { set; get; }
//闭塞
public string occlusion { set; get; }
}