Json对象处理.将对象处理成dic数组.

var parser = new JsonConfigurationFileParser();
var dict = parser.Parse("json");
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo
{
internal class JsonConfigurationFileParser
{
private readonly IDictionary<string, string> _data = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Stack<string> _context = new Stack<string>();
private string _currentPath;
public IDictionary<string, string> Parse(string content)
{
_data.Clear();
var jsonConfig = JObject.Parse(content);
VisitJObject(jsonConfig);
return _data;
}
private void VisitJObject(JObject jObject)
{
foreach (var property in jObject.Properties())
{
EnterContext(property.Name);
VisitProperty(property);
ExitContext();
}
}
private void VisitProperty(JProperty property)
{
VisitToken(property.Value);
}
private void VisitToken(JToken token)
{
switch (token.Type)
{
case JTokenType.Object:
VisitJObject(token.Value<JObject>());
break;
case JTokenType.Array:
VisitArray(token.Value<JArray>());
break;
case JTokenType.Integer:
case JTokenType.Float:
case JTokenType.String:
case JTokenType.Boolean:
case JTokenType.Bytes:
case JTokenType.Raw:
case JTokenType.Null:
VisitPrimitive(token);
break;
default:
throw new FormatException("FormatError_UnsupportedJSONToken");
}
}
private void VisitArray(JArray array)
{
for (int index = 0; index < array.Count; index++)
{
EnterContext(index.ToString());
VisitToken(array[index]);
ExitContext();
}
}
private void VisitPrimitive(JToken data)
{
var key = _currentPath;
if (_data.ContainsKey(key))
{
throw new FormatException("FormatError_KeyIsDuplicated");
}
_data[key] = data.ToString();
}
private void EnterContext(string context)
{
_context.Push(context);
_currentPath = ConfigurationPath.Combine(_context.Reverse());
}
private void ExitContext()
{
_context.Pop();
_currentPath = ConfigurationPath.Combine(_context.Reverse());
}
}
}
Json对象处理.将对象处理成dic数组.的更多相关文章
- Spring MVC 后端获取前端提交的json格式字符串并直接转换成control方法对应的参数对象
场景: 在web应用开发中,spring mvc凭借出现的性能和良好的可扩展性,导致使用日渐增多,成为事实标准,在日常的开发过程中,有一个很常见的场景:即前端通过ajax提交方式,提交参数为一个jso ...
- 如何将PHP对象数组转换成普通数组
/** * 对象数组转为普通数组 * * AJAX提交到后台的JSON字串经decode解码后为一个对象数组, * 为此必须转为普通数组后才能进行后续处理, * 此函数支持多维数组处理. * * @p ...
- JSON字符串与java对象的转换
所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...
- JSON 字符串 与 java 对象的转换
jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //j ...
- .NET中如何使用反序列化JSON字符串/序列化泛型对象toJsonStr
在进行 .NET Web MVC 框架开发的网站程序的时候,我们都会遇到最关键的问题,数据传输. .NET MVC 4中的ControllerBase类建议我们用ViewBag动态数据字典形式(t ...
- Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mengzhengbin520.blog.51cto.com/7590564/12 ...
- Java:Json与其他Java对象集合的转换
一.引入的jar包 json-lib-2.4-jdk15.jar 二.Json字符串转换为其他对象 1.对象==>json字符串 2.list和Map集合==>json字符串 3.Map集 ...
- 多层次的Json字符串转化为对象
using Arvato.CRM.DataTrans.ConsoleHost.Model;using System;using System.Collections.Generic;using Sys ...
- 用JAVA进行Json数据解析(对象数组的相互嵌套)
这段时间我们在做一个英语翻译软件的小小小APP,涉及到了对Json数据的解析,所以特地来总结一下! 假设我们要对如下数据进行解析,其实在平时,返回的Json数据是很乱的,很难分清数据的关系,这是经过相 ...
随机推荐
- struts2项目搭建
把strutslib中的所有jar包添加到类路径 在src下创建struts.xml文件 <?xml version="1.0" encoding="UTF-8&q ...
- webstorm加载项目卡死在scanning files to index
今天用webstorm导入项目时,需要加载node-modules文件夹,导致webstorm非常卡,页面提示scanning files to index... 网上搜到办法,记录下: 说明: 在n ...
- 030_CORS深究
在日常的项目开发时会不可避免的需要进行跨域操作,而在实际进行跨域请求时,经常会遇到类似 No 'Access-Control-Allow-Origin' header is present on th ...
- win10:家庭版开启组策略
1.新建一个txt文件 2.复制以下内容到txt文件 @echo off pushd "%~dp0" dir /b C:\Windows\servicing\Packages\Mi ...
- 滑动时候警告:Unable to preventDefault inside passive event listener
1 前言 在制作2048时,需要在手机端添加滑动检测事件,然后发现控制台有警告,如下: main2048.js:218 [Intervention] Unable to preventDefault ...
- HTML网页背景图很长要有滚动条滑动
1 前言 由于网页背景图非常长,然后会出现只显示当前页面,后面部分看不到,也不能滑动,开始以为不能滑动应该是没有加overflow-y: auto(scroll),结果加上去也是无效的. 2 代码 & ...
- layui 各种弹出框
第一步:引用文件 第二步:写脚本 <script> //iframe窗 //layer.open({ // type: 2, // title: false, // closeBtn: 0 ...
- FileStorage
1. 函数说明 功能 函数声明 参数 FileStorage构造函数 cv::FileStorage:: FileStorage(const String& ...
- Confluence 6 使用页面请求属性来对慢性能进行问题解决
这个页面告诉你如何启用页面请求属性.当这个属性启用以后,你可以查看在 Confluence 任何页面完成的一个记录消耗的时间(毫秒).如果 Confluence 反应缓慢的话,一个关于慢页面请求的内部 ...
- 动态获取后台传过来的值作为select选项
问题描述:点击左侧菜单项,进入对应的具体页面a.html,页面上方有个select框,点击框后,会浮现选择项. 解决思路:对左侧菜单项添加一个onclick事件,进入后台做具体的查询,将查询到的lis ...