C# 操作JSON的几种方式
本篇通过两种在c#中常用的方式来实现对Json数据的序列化和反序列化,为了实现大多数的需求,我们采用稍微复杂一点的数据模型。
首先我们有以下json数据
{
"name": "张三",
"age": 20,
"idCard": "123456789",
"birthday": "2021-01-01 00:00:00",
"hobbys": [
{
"sort": 1,
"desc": "写字"
},
{
"sort": 2,
"desc": "游泳"
}
]
}
然后我们根据json的结构创建一个实体类
private class Studet
{
public string name { get; set; }
public int age { get; set; }
public string idCard { get; set; }
public DateTime birthday { get; set; }
public List<hobby> hobbys { get; set; }
}
private class hobby
{
public int sort { get; set; }
public string desc { get; set; }
}
第一种方式:JavaScriptSerializer
//需添加引用:System.Web.Extensions
//需导入命名空间:using System.Web.Script.Serialization;
//json.txt中的数据为上面的json数据
string json = File.ReadAllText("d:\\json.txt");
JavaScriptSerializer jss = new JavaScriptSerializer();
//反序列化成Studet对象
Studet student = jss.Deserialize<Studet>(json);
//然后就可以直接用操作对象的方法直接点出来就行
//输出:张三
Console.WriteLine(student.name);
//同样的,我们可以将上面的对象序列化成一个json数据
//序列化成json
string resultJson = jss.Serialize(student);
Console.WriteLine(resultJson);
第二种方式:JSON.NET
//需添加引用:Newtonsoft.Json
//需导入命名空间:using Newtonsoft.Json; using Newtonsoft.Json.Linq;
//反序列化成对象
Studet student = JsonConvert.DeserializeObject<Studet>(json);
//输出:张三
Console.WriteLine(student.name);
//序列化成json
string resultJson = JsonConvert.SerializeObject(student);
Console.WriteLine(resultJson);
以上是我们通过json结构映射实体类的方式去实现的解析json数据,很多时候我们可能并不需要,或者并不想创建太多的类,这时候我们可以通过以下方式去实现解析
//使用JObject(JSON.NET)
//反序列化成JObject
JObject jobj = JObject.Parse(json);
//输出:张三
Console.WriteLine(jobj["name"].ToString());
//解析hobbys
JArray jarr = JArray.Parse(jobj["hobbys"].ToString());
//迭代jarr即可
//同样的,假如我们有一个JObject对象
JObject obj = new JObject();
obj["name"] = "张三";
obj["age"] = 20;
//输出json数据,直接ToString()就可以
Console.WriteLine(obj.ToString());
如果说你的Json结构是一个数组形式的话,即[{},{},{}],我们可以通过以下方式去解析
//第一种:DataTable
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);
//或者使用JavaScriptSerializer
dt =jss.Deserialize<DataTable>(json);
//解析DataTable即可
//第二种:JArray
JArray jarr = JArray.Parse(json);
//然后去迭代jarr即可
foreach (var j in jarr)
{
Console.WriteLine(j["key"]);
}
//第三种,也就是我们上面提到了有实体类的情况下
List<Studet> list = JsonConvert.DeserializeObject<List<Studet>>(json);
以上基本可以解决我们的日常开发需求,个人推荐使用JSON.NET,下一篇会再介绍下JSON.NET的部分高级用法,用来满足我们的一些特殊需求。
JObject joDest = (JObject)JsonConvert.DeserializeObject(dest);
JObject joSource = (JObject)JsonConvert.DeserializeObject(src);
String s1= joSource["frameOfReferenceUID"].ToString();
double[] s2 = joSource["imagePositionPatient"].ToObject<List<double>>().ToArray();
double[] s3= joSource["imageOrientationPatient"].ToObject<List<double>>().ToArray();
double[] s4= joSource["pixelSpacing"].ToObject<List<double>>().ToArray();
int s5= joSource["columns"].ToObject<int>();
int s6= joSource["rows"].ToObject<int>();
// DicoomFrame destDicoomFrame = JsonConvert.DeserializeObject<DicoomFrame>("");
// DicoomFrame sourceDicoomFrame = JsonConvert.DeserializeObject<DicoomFrame>("");
FrameGeometry destFrame = new FrameGeometry(joDest["frameOfReferenceUID"].ToString(), joDest["imagePositionPatient"].ToObject<List<double>>().ToArray(), joDest["imageOrientationPatient"].ToObject<List<double>>().ToArray(), joDest["pixelSpacing"].ToObject<List<double>>().ToArray(), joDest["columns"].ToObject<int>(), joDest["rows"].ToObject<int>());
FrameGeometry sourceFrame = new FrameGeometry(joSource["frameOfReferenceUID"].ToString(), joSource["imagePositionPatient"].ToObject<List<double>>().ToArray(), joSource["imageOrientationPatient"].ToObject<List<double>>().ToArray(), joSource["pixelSpacing"].ToObject<List<double>>().ToArray(), joSource["columns"].ToObject<int>(), joSource["rows"].ToObject<int>());
//(string frameOfReferenceUid, double[] imagePatientPosition, double[] imagePatientOrientation, double[] pixelSpacing, int width, int height);
/* String destFrameOfReferenceUid = "1.2.840.113619.2.417.3.2831214628.890.1659999615.388.10305.1";
double[] destImagePatientPosition = new double[] {0.000, 235.000, 150.000};
double[] destImagePatientOrientation = new double[] { 0.000000,-1.000000,0.000000,0.000000,0.000000,-1.000000 };
double[] destPixelSpacing = new double[] { 0.545455,0.691176 };
int destWidth = 551 ;
int destHeight = 680;
FrameGeometry destFrame = new FrameGeometry(destFrameOfReferenceUid, destImagePatientPosition, destImagePatientOrientation, destPixelSpacing, destWidth, destHeight);
String sourceFrameOfReferenceUid = "1.2.840.113619.2.417.3.2831214628.890.1659999615.388.10305.1";
double[] sourceImagePatientPosition = new double[] { -75.000,-27.800,2.000 };
double[] sourceImagePatientOrientation = new double[] { 1.000000,0.000000,0.000000,0.000000,1.000000,0.000000 };
double[] sourcePixelSpacing = new double[] { 0.292969,0.292969 };
int sourceWidth = 512;
int sourceHeight = 512;
FrameGeometry sourceFrame = new FrameGeometry(sourceFrameOfReferenceUid, sourceImagePatientPosition, sourceImagePatientOrientation, sourcePixelSpacing, sourceWidth, sourceHeight);
*/
Point2 start = new Point2();
Point2 end=new Point2();
if (CanDrawLocalizer(sourceFrame, destFrame)) {
CalcualteIntersectionLocalizer(sourceFrame, destFrame, out start, out end);
}
if (javascriptCallback != null)
{
Task.Factory.StartNew(async () =>
{
using (javascriptCallback)
{
await javascriptCallback.ExecuteAsync(start.X,start.Y,end.X,end.Y);
}
});
}
JObject result = new JObject();
JObject startPoint = new JObject();
JObject endPoint = new JObject();
startPoint["x"] = start.X;
startPoint["y"] = start.Y;
endPoint["x"] = end.X;
endPoint["y"] = end.Y;
result["start"] = startPoint;
result["end"] = endPoint;
return result.ToString();
————————————————
版权声明:本文为CSDN博主「Csharp 小记」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_27410185/article/details/120819917
更多推荐
所有评论(0)