Newtonsoft.Json.Linq.JObject 遍历验证每个属性内容
业务需求,拦截器验证每个请求inputstream(实际是application/json流)的数据,但是json反序列化实体格式不同。
var req = filterContext.RequestContext.HttpContext.Request;
if (req.ContentType.ToLower().Contains("application/json") && req.InputStream.Length > )
{
System.IO.Stream stm = new MemoryStream();
req.InputStream.CopyTo(stm);
stm.Position = ;
req.InputStream.Position = ;
using (System.IO.StreamReader sr = new System.IO.StreamReader(stm))
{
try
{
Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Parse(sr.ReadToEnd());
if (jo.HasValues)
{
foreach (JToken item in jo.Values())
{
var tmpMsg = "";
int ckResult = ChkJson(item, out tmpMsg);
if (ckResult != )
{
Content.Content = tmpMsg;
filterContext.Result = Content;
filterContext.HttpContext.Response.StatusCode = ckResult;
filterContext.HttpContext.Response.StatusDescription = "sensitive information";
return;
}
}
}
}
catch (System.Exception)
{
// 若输入流不是json对象不再校验
} }
}
protected new int ChkJson(JToken jo, out string msg)
{
msg = "";
if (jo == null) return ;
if (jo.HasValues && jo.Values().Count() > )
{
foreach (JToken item in jo.Values())
{
var result = ChkJson(item, out msg);
if (result != )
return result;
}
}
else
{
string val = jo.ToString();
if (IsContainXSSCharacter(val , out msg)){
return ;
}
} return ;
}
Newtonsoft.Json.Linq.JObject 遍历验证每个属性内容的更多相关文章
- 遍历Newtonsoft.Json.Linq.JObject
JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...
- Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.
https://blog.csdn.net/zhouyingge1104/article/details/83307637 C#项目中使用NewtonSoft.json,报错提示: Can not a ...
- asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported
在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误: The collection type 'Newtonsoft.Json.Linq ...
- 如何遍历newtonsoft.json的JObject里的JSON数据
这种问题,在网上搜,居然没有答案,又是一堆垃圾,连谷歌上都搜不到.老实说,我喜欢这边的工作环境,可以上谷歌,毕竟是大公司,有自己的VPN .某组织整天禁这个禁那个,去年居然连谷歌都禁了,丧心病狂至此, ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...
- Newtonsoft.Json 通过 JObject 读取 json对像 超简单
/* json 格式的字符串解析 格式化 { "input": { "size": 193156, "type": "image/ ...
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- Newtonsoft.Json.Linq 常用方法总结
目录 1.Entity to Json 1.1.准备工作 1.2.Entity to Json 1.3.Json to Entity 2.Linq To Json 2.1.创建对象 2.2.从 Jso ...
- Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称
手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...
随机推荐
- DOS批处理中%~dp0表示什么意思
https://www.jianshu.com/p/5a1a882ead95 https://www.cnblogs.com/cnpirate/p/5282324.html https://www.c ...
- spring boot启动无法访问controller
如题,检测 一.启动类Application是不是在最上一级,默认扫描启动类平级和下级目录的bean 二.启用内置Tomcat,注释掉 scope <dependency> <gro ...
- Struts2访问Servlet
知识点: servlet是单例的,Action是多例的,一次请求,创建一个Action的实例 结果页面分为全局和局部两类(局部优先级更高) result标签:name : 默认succestype : ...
- MySQL高效分页-mybatis插件PageHelper改进
MySQL分页在表比较大的时候,分页就会出现性能问题,MySQL的分页逻辑如下:比如select * from user limit 100000,10 它是先执行select * from user ...
- spark 读写text,csv,json,parquet
以下代码演示的是spark读取 text,csv,json,parquet格式的file 为dataframe, 将dataframe保存为对应格式的文件 package com.jason.spar ...
- django缓存相关
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_39726347/articl ...
- elasticsearch 常见查询及聚合的JAVA API
ES 常见查询 (1)根据ID 进行单个查询 GetResponse response = client.prepareGet("accounts", "person&q ...
- [转帖][区块链]共识算法(POW,POS,DPOS,PBFT)介绍和心得
[区块链]共识算法(POW,POS,DPOS,PBFT)介绍和心得 置顶 2017-03-12 18:31:19 乐扣老师lekkoliu 阅读数 127953 收藏 更多 分类专栏: 技术管理 区 ...
- 【转帖】Spark设计理念与基本架构
Spark设计理念与基本架构 https://www.cnblogs.com/swordfall/p/9280006.html 1.基本概念 Spark中的一些概念: RDD(resillient d ...
- Java的访问修饰符的作用范围
访问修饰符: private default protected public 作用范围: 访问修饰符\作用范围 所在类 同一包内其他类 其他包内子类 其他包内非子类 private 可以访问 不可以 ...