为了在返回 json 对象时不出错或者能够正确解析它,应该怎么做?
我现在是一名实习生。我刚刚学习编码过程。我将数据作为 json 对象返回。我的开发经理说应该做一个查询或者操作,这样解析Json对象的时候就没有问题了。可以采取什么行动?
他说这应该在包裹处理之前完成。我想到了try catch方法。但我不认为这就是最终的结果。在解析操作之前我应该编写什么样的查询或代码?这里我将excel中的数据作为json返回。
我的代码:
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(_appSetting.ExcelLicense);
byte[] excelData = System.IO.File.ReadAllBytes(fileName);
MemoryStream uploadExcelStream = new MemoryStream(excelData);
//Stream uploadExcelStream = data.File.OpenReadStream();
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = Path.GetExtension(fileName) == "xlsx" ? ExcelVersion.Xlsx : ExcelVersion.Excel97to2003;
IWorkbook book = application.Workbooks.Open(uploadExcelStream);
uploadExcelStream.Close();
MemoryStream jsonStream = new MemoryStream();
book.SaveAsJson(jsonStream, true);
excelEngine.Dispose();
byte[] json = new byte[jsonStream.Length];
jsonStream.Position = 0;
jsonStream.Read(json, 0, (int)jsonStream.Length);
string jsonString = Encoding.UTF8.GetString(json, 0, json.Length);
JObject jsonObject = JObject.Parse(jsonString);
var json2 = JsonConvert.SerializeObject(jsonObject);
return Json(json2, "application/json");
从 Excel 转换为 json 时,我被告知有时解析过程可能会出现问题,我需要在其上方编写一个查询。我怎样做才能不出现问题呢?
I am an intern now. I'm just learning coding processes. I am returning a data as json object. My developer manager said that a query or operation should be done so that there is no problem when parsing the Json object. What action can be taken?
He said it should be done before the parcel process. I thought of the try catch method. But I don't think this is the definitive result. What kind of query or code should I write before the parse operation? Here I am returning the data in excel as json.
My code:
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(_appSetting.ExcelLicense);
byte[] excelData = System.IO.File.ReadAllBytes(fileName);
MemoryStream uploadExcelStream = new MemoryStream(excelData);
//Stream uploadExcelStream = data.File.OpenReadStream();
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = Path.GetExtension(fileName) == "xlsx" ? ExcelVersion.Xlsx : ExcelVersion.Excel97to2003;
IWorkbook book = application.Workbooks.Open(uploadExcelStream);
uploadExcelStream.Close();
MemoryStream jsonStream = new MemoryStream();
book.SaveAsJson(jsonStream, true);
excelEngine.Dispose();
byte[] json = new byte[jsonStream.Length];
jsonStream.Position = 0;
jsonStream.Read(json, 0, (int)jsonStream.Length);
string jsonString = Encoding.UTF8.GetString(json, 0, json.Length);
JObject jsonObject = JObject.Parse(jsonString);
var json2 = JsonConvert.SerializeObject(jsonObject);
return Json(json2, "application/json");
When converting from Excel to json, I was told that sometimes there may be a problem with the parse process and I need to write a query just above it. How can I do something so that there is no problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为你可以稍微简化一下上面的部分:
至于你的问题,有点不清楚你正在尝试/应该做什么。
由于您首先对传入的 json 进行反序列化,然后再次序列化它以返回,所以我只能假设您应该在这两个操作之间对对象进行某种检查。
First up I think you could simplify the top part a little bit:
As to your question, it´s a bit unclear what you are trying/supposed to do.
Since you are first desirializing your incoming json and then serialize it again to return, I can only assume you are supposed to do some kind of check on the object in between those two actions.