c#迭代遍历带数组的json格式数据
【1】首先我们先创建一个带数组形式的json格式的数组
1)我们按照结构定义一个类,如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ceshi1
{
class Data
{
public string text
{
get;
set;
}
public int nodeType
{
get;
set;
}
public int nodeID
{
get;
set;
}
public bool leaf
{
get;
set;
}
public int devicedID
{
get;
set;
}
public List<Data> children
{
get;
set;
}
}
}
【2】我们来创造生成一个结构
//定义一个数据结构
Data data = new Data();
data.nodeType = 1;
data.nodeID = 10;
data.leaf = false;
data.devicedID = 10;
data.text = "直流监测";
data.children = new List<Data>(){
new Data(){
nodeType = 11,
nodeID = 101,
leaf = false,
devicedID = 101,
text = "广州局",
children = new List<Data>(){
new Data(){
nodeType = 111,
nodeID = 1011,
leaf = false,
devicedID = 1011,
text = "从化站",
children = new List<Data>(){
new Data(){
nodeType = 1111,
nodeID = 10111,
leaf = true,
devicedID = 10111,
text = "从化站#1主变",
children = new List<Data>(){}
},
new Data(){
nodeType = 1112,
nodeID = 10112,
leaf = true,
devicedID = 10112,
text = "从化站#2主变",
children = new List<Data>(){}
}
}
},
new Data(){
nodeType = 112,
nodeID = 1012,
leaf = false,
devicedID = 1012,
text = "板桥站",
children = new List<Data>(){
new Data(){
nodeType = 1121,
nodeID = 10121,
leaf = true,
devicedID = 1021,
text = "板桥站#1主变",
children = new List<Data>(){}
}
}
}
}
},
new Data(){
nodeType = 12,
nodeID = 102,
leaf = false,
devicedID = 102,
text = "深圳局",
children = new List<Data>(){
new Data(){
nodeType = 121,
nodeID = 1021,
leaf = false,
devicedID = 1021,
text = "福田站",
children = new List<Data>(){
new Data(){
nodeType = 1211,
nodeID = 10211,
leaf = true,
devicedID = 10211,
text = "板桥站#1主变",
children = new List<Data>(){}
}
}
}
}
},
new Data(){
nodeType = 13,
nodeID = 103,
leaf = false,
devicedID = 103,
text = "珠海局",
children = new List<Data>(){
new Data(){}
}
}
};
Console.WriteLine(data);
我们打印出来的对象为:ceshi1.Data;它是以一个对象的形式呈现;
【3】我们附加一点字符串/对象/json格式的转换方法讲解
首先引入第三方库:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
<a>将data序列化为json字符串
string json = JsonConvert.SerializeObject(data);
Console.WriteLine(json);
打印结果为(字符串):
{"text":"直流监测","nodeType":1,"nodeID":10,"leaf":false,"devicedID":10,"children":[{"text":"广州局","nodeType":11,"nodeID":101,"leaf":false,"devicedID":101,"children":[{"text":"从化站","nodeType":111,"nodeID":1011,"leaf":false,"devicedID":1011,"children":[{"text":"从化站#1主变","nodeType":1111,"nodeID":10111,"leaf":true,"devicedID":10111,"children":[]},{"text":"从化站#2主变","nodeType":1112,"nodeID":10112,"leaf":true,"devicedID":10112,"children":[]}]},{"text":"板桥站","nodeType":112,"nodeID":1012,"leaf":false,"devicedID":1012,"children":[{"text":"板桥站#1主变","nodeType":1121,"nodeID":10121,"leaf":true,"devicedID":1021,"children":[]}]}]},{"text":"深圳局","nodeType":12,"nodeID":102,"leaf":false,"devicedID":102,"children":[{"text":"福田站","nodeType":121,"nodeID":1021,"leaf":false,"devicedID":1021,"children":[{"text":"板桥站#1主
变","nodeType":1211,"nodeID":10211,"leaf":true,"devicedID":10211,"children":[]}]}]},{"text":"珠海局","nodeType":13,"nodeID":103,"leaf":false,"devicedID":103,"children":[{"text":null,"nodeType":0,"nodeID":0,"leaf":false,"devicedID":0,"children":null}]}]}
<b>有序列化,肯定有反序列化,将json反序列化为data,反序列化为对象
Data jsonData = JsonConvert.DeserializeObject<Data>(json);
Console.WriteLine(data);
打印结果为(对象):
ceshi1.Data;
<c>将json字符串格式化
JObject jo = (JObject)JsonConvert.DeserializeObject(json);
Console.WriteLine(jo);
打印结果为(json格式):
{
"text": "直流监测",
"nodeType": 1,
"nodeID": 10,
"leaf": false,
"devicedID": 10,
"children": [
{
"text": "广州局",
"nodeType": 11,
"nodeID": 101,
"leaf": false,
"devicedID": 101,
"children": [
{
"text": "从化站",
"nodeType": 111,
"nodeID": 1011,
"leaf": false,
"devicedID": 1011,
"children": [
{
"text": "从化站#1主变",
"nodeType": 1111,
"nodeID": 10111,
"leaf": true,
"devicedID": 10111,
"children": []
},
{
"text": "从化站#2主变",
"nodeType": 1112,
"nodeID": 10112,
"leaf": true,
"devicedID": 10112,
"children": []
}
]
},
{
"text": "板桥站",
"nodeType": 112,
"nodeID": 1012,
"leaf": false,
"devicedID": 1012,
"children": [
{
"text": "板桥站#1主变",
"nodeType": 1121,
"nodeID": 10121,
"leaf": true,
"devicedID": 1021,
"children": []
}
]
}
]
},
{
"text": "深圳局",
"nodeType": 12,
"nodeID": 102,
"leaf": false,
"devicedID": 102,
"children": [
{
"text": "福田站",
"nodeType": 121,
"nodeID": 1021,
"leaf": false,
"devicedID": 1021,
"children": [
{
"text": "板桥站#1主变",
"nodeType": 1211,
"nodeID": 10211,
"leaf": true,
"devicedID": 10211,
"children": []
}
]
}
]
},
{
"text": "珠海局",
"nodeType": 13,
"nodeID": 103,
"leaf": false,
"devicedID": 103,
"children": [
{
"text": null,
"nodeType": 0,
"nodeID": 0,
"leaf": false,
"devicedID": 0,
"children": null
}
]
}
]
}
到这一步,数据已经生成了,各位看官想要的三种数据形式都已经有了,接下来就是我们的重头戏了;
【4】迭代遍历取节点
需求:取出最底层的leaf = true或者Children为null的情况的节点所有的nodeType/nodeID/devicedID/text值;并以数组或者list的方式返回
a)下面我们定义一个迭代方法:
public static void FindAll(Data inputData, ref List<Data> data)
{
if (inputData.leaf)
{
data.Add(inputData);
}
else
{
if (inputData.children == null) return;
foreach (var item in inputData.children)
{
if (item.leaf)
{
data.Add(item);
}
else
{
FindAll(item,ref data);
}
}
}
}
b)我们在main函数(或者需要的地方)中调用,加上如下代码:
//序列化为json字符串
string json = JsonConvert.SerializeObject(data);
//Console.WriteLine(json);
//反序列化为对象
Data jsonData = JsonConvert.DeserializeObject<Data>(json);
//Console.WriteLine(data);
//json格式化
JObject jo = (JObject)JsonConvert.DeserializeObject(json);
//Console.WriteLine(jo);
//问题:
//取出最底层的leaf = true或者Children为空的情况的节点所有的nodeType/nodeID/devicedID/text值?
//需要迭代遍历?
var resultData = new List<Data>();
FindAll(data, ref resultData);
Console.ReadKey();
【5】检验是否正确
到这一步,我们基本上已经完成了,我们来测试一下最终的结果,遍历一下这个返回结果resultData
for (int i = 0; i < resultData.Count;i++ )
{
Console.WriteLine(JsonConvert.SerializeObject(resultData[i]));
}
输出结果如下:
{"text":"从化站#1主变","nodeType":1111,"nodeID":10111,"leaf":true,"devicedID":10111,"children":[]}
{"text":"从化站#2主变","nodeType":1112,"nodeID":10112,"leaf":true,"devicedID":10112,"children":[]}
{"text":"板桥站#1主变","nodeType":1121,"nodeID":10121,"leaf":true,"devicedID":1021,"children":[]}
{"text":"板桥站#1主变","nodeType":1211,"nodeID":10211,"leaf":true,"devicedID":10211,"children":[]}
显然,取出了全部的叶子结点
【6】结果源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ceshi1
{
class Program
{
//public ArrayList al = new ArrayList();
//public void GetAllDirList(string strBaseDir)
//{
// DirectoryInfo di = new DirectoryInfo(strBaseDir);
// DirectoryInfo[] dirA = di.GetDirectories();
// for (int i = 0; i < dirA.Length; i++)
// {
// al.Add(dirA[i].FullName);
// Console.WriteLine(dirA[i]);
// Console.WriteLine(dirA[i].FullName);
// GetAllDirList(dirA[i].FullName);
// }
//}
static void Main(string[] args)
{
//定义一个数据结构
Data data = new Data();
data.nodeType = 1;
data.nodeID = 10;
data.leaf = false;
data.devicedID = 10;
data.text = "直流监测";
data.children = new List<Data>()
{
new Data()
{
nodeType = 11,
nodeID = 101,
leaf = false,
devicedID = 101,
text = "广州局",
children = new List<Data>()
{
new Data()
{
nodeType = 111,
nodeID = 1011,
leaf = false,
devicedID = 1011,
text = "从化站",
children = new List<Data>()
{
new Data()
{
nodeType = 1111,
nodeID = 10111,
leaf = true,
devicedID = 10111,
text = "从化站#1主变",
children = new List<Data>() {}
},
new Data()
{
nodeType = 1112,
nodeID = 10112,
leaf = true,
devicedID = 10112,
text = "从化站#2主变",
children = new List<Data>() {}
}
}
},
new Data()
{
nodeType = 112,
nodeID = 1012,
leaf = false,
devicedID = 1012,
text = "板桥站",
children = new List<Data>()
{
new Data()
{
nodeType = 1121,
nodeID = 10121,
leaf = true,
devicedID = 1021,
text = "板桥站#1主变",
children = new List<Data>() {}
}
}
}
}
},
new Data()
{
nodeType = 12,
nodeID = 102,
leaf = false,
devicedID = 102,
text = "深圳局",
children = new List<Data>()
{
new Data()
{
nodeType = 121,
nodeID = 1021,
leaf = false,
devicedID = 1021,
text = "福田站",
children = new List<Data>()
{
new Data()
{
nodeType = 1211,
nodeID = 10211,
leaf = true,
devicedID = 10211,
text = "板桥站#1主变",
children = new List<Data>() {}
}
}
}
}
},
new Data()
{
nodeType = 13,
nodeID = 103,
leaf = false,
devicedID = 103,
text = "珠海局",
children = new List<Data>()
{
new Data() {}
}
}
};
//序列化为json字符串
string json = JsonConvert.SerializeObject(data);
//Console.WriteLine(json);
//反序列化为对象
Data jsonData = JsonConvert.DeserializeObject<Data>(json);
//Console.WriteLine(data);
//json格式化
JObject jo = (JObject)JsonConvert.DeserializeObject(json);
//Console.WriteLine(jo);
//问题:
//取出最底层的leaf = true或者Children为空的情况的节点所有的nodeType/nodeID/devicedID/text值?
//需要迭代遍历?
var resultData = new List<Data>();
FindAll(data, ref resultData);
//Console.WriteLine(resultData);
for (int i = 0; i < resultData.Count;i++ )
{
Console.WriteLine(JsonConvert.SerializeObject(resultData[i]));
}
Console.ReadKey();
}
public static void FindAll(Data inputData, ref List<Data> data)
{
if (inputData.leaf)
{
data.Add(inputData);
}
else
{
if (inputData.children == null) return;
foreach (var item in inputData.children)
{
if (item.leaf)
{
data.Add(item);
}
else
{
FindAll(item,ref data);
}
}
}
}
}
}
【6】监测过程源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Data;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace ceshi1
{
class Program
{
//public ArrayList al = new ArrayList();
//public void GetAllDirList(string strBaseDir)
//{
// DirectoryInfo di = new DirectoryInfo(strBaseDir);
// DirectoryInfo[] dirA = di.GetDirectories();
// for (int i = 0; i < dirA.Length; i++)
// {
// al.Add(dirA[i].FullName);
// Console.WriteLine(dirA[i]);
// Console.WriteLine(dirA[i].FullName);
// GetAllDirList(dirA[i].FullName);
// }
//}
static void Main(string[] args)
{
//定义一个数据结构
Data data = new Data();
data.nodeType = 1;
data.nodeID = 10;
data.leaf = false;
data.devicedID = 10;
data.text = "直流监测";
data.children = new List<Data>(){
new Data(){
nodeType = 11,
nodeID = 101,
leaf = false,
devicedID = 101,
text = "广州局",
children = new List<Data>(){
new Data(){
nodeType = 111,
nodeID = 1011,
leaf = false,
devicedID = 1011,
text = "从化站",
children = new List<Data>(){
new Data(){
nodeType = 1111,
nodeID = 10111,
leaf = true,
devicedID = 10111,
text = "从化站#1主变",
children = new List<Data>(){}
},
new Data(){
nodeType = 1112,
nodeID = 10112,
leaf = true,
devicedID = 10112,
text = "从化站#2主变",
children = new List<Data>(){}
}
}
},
new Data(){
nodeType = 112,
nodeID = 1012,
leaf = false,
devicedID = 1012,
text = "板桥站",
children = new List<Data>(){
new Data(){
nodeType = 1121,
nodeID = 10121,
leaf = true,
devicedID = 1021,
text = "板桥站#1主变",
children = new List<Data>(){}
}
}
}
}
},
new Data(){
nodeType = 12,
nodeID = 102,
leaf = false,
devicedID = 102,
text = "深圳局",
children = new List<Data>(){
new Data(){
nodeType = 121,
nodeID = 1021,
leaf = false,
devicedID = 1021,
text = "福田站",
children = new List<Data>(){
new Data(){
nodeType = 1211,
nodeID = 10211,
leaf = true,
devicedID = 10211,
text = "板桥站#1主变",
children = new List<Data>(){}
}
}
}
}
},
new Data(){
nodeType = 13,
nodeID = 103,
leaf = false,
devicedID = 103,
text = "珠海局",
children = new List<Data>(){
new Data(){}
}
}
};
//序列化为json字符串
string json = JsonConvert.SerializeObject(data);
// Console.WriteLine(json);
//反序列化为对象
Data jsonData = JsonConvert.DeserializeObject<Data>(json);
//取出最底层的leaf = true或者Children为空的情况的节点所有的nodeType/nodeID/devicedID/text值?
FindAll(jsonData);
Console.ReadKey();
}
public static void FindAll(Data inputData)
{
if (inputData.leaf)
{
Console.WriteLine("leaf:{0}, nodeType:{1}, nodeID:{2}, devicedID:{3}, text: {4}, children is null:{5}"
, inputData.leaf
, inputData.nodeType
, inputData.nodeID
, inputData.devicedID
, inputData.text
, inputData.children == null);
}
else
{
if (inputData.children == null) return;
foreach (var item in inputData.children)
{
if (item.leaf)
{
Console.WriteLine(
"leaf:{0}, nodeType:{1}, nodeID:{2}, devicedID:{3}, text: {4}, children is null:{5}"
, item.leaf
, item.nodeType
, item.nodeID
, item.devicedID
, item.text
, item.children == null);
//我想把取出来结果以对象的方式放到数组或者一个List集合里面去,然后再返回这个集合或者数组(最好用数组),谢谢
//数组类似于:[{leaf:True, nodeType:1111, nodeID:10111, devicedID:10111,
text: 从化站#1主变, children is null:False},{leaf:True, nodeType:1112,
nodeID:10112, devicedID:10112, text: 从化站#2主变, children is null:False}]
}
else
{
FindAll(item);
}
}
}
}
}
}
本文源于zhuxiaoge(http://www.cnblogs.com/zhuxiaoge/p/7090544.html),如有转载请标明出处,不甚感激!!!
c#迭代遍历带数组的json格式数据的更多相关文章
- JS学习笔记(3)--json格式数据的添加,删除及排序方法
这篇文章主要介绍了json格式数据的添加,删除及排序方法,结合实例形式分析了针对一维数组与二维数组的json格式数据进行增加.删除与排序的实现技巧,需要的朋友可以参考下 本文实例讲述了json格式 ...
- fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map
作者:buster2014 推荐:长安散人 fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map JSON字符串与Java对象的转换 1.将Java对象或Java ...
- JSon_零基础_002_将List类型数组转换为JSon格式的对象字符串,返回给界面
将List类型数组转换为JSon格式的对象字符串,返回给界面 所需要导入的包: 编写bean: package com.west.webcourse.po; /** * 第01步:编写bean类, * ...
- JSon_零基础_001_将布尔类型数组转换为JSon格式字符串,返回给界面
将布尔类型数组转换为JSon格式字符串,返回给界面 需要导入包: 编写bean: package com.west.webcourse.po; /** * 第01步:编写bean类, * 下一步com ...
- ajax接收遍历处理json格式数据
ajax在前后端的交互中应用非常广泛,通过请求后台接口接收处理json格式数据展现在前端页面. 下面我们来简单用 ajax在本地做一个接收并处理json的小例子 首先我们要新建一个叫做data的jso ...
- Python:numpy数组转换为json格式
在python中,如何将一个numpy数组转换为json格式? 这是最近遇到的一个问题,做个笔记. 假设arr为numpy数组,将其转换为json格式: 总体思想是①首先转换为python的list, ...
- iOS开发之JSON格式数据的生成与解析
本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...
- 解析json格式数据
实现目标 读取文件中的json格式数据,一行为一条json格式数据.进行解析封装成实体类. 通过google的Gson对象解析json格式数据 我现在解析的json格式数据为: {",&qu ...
- Spring MVC 学习笔记11 —— 后端返回json格式数据
Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...
随机推荐
- windows监控 排查蓝屏问题
DUMP包分析 工具WinDbg,下载打开,按ctrl+S 输入:SRV*C:\websymbols*http://msdl.microsoft.com/download/symbols:点击OK, ...
- spring 构造方法注入和setter方法注入的XML表达
1.构造方法注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC ...
- 大数进制转换 poj1220
普通的做法,大数除小数. 复杂度o( log(n)*log(n) ),其实就是位数的平方. NUMBER BASE CONVERSION Time Limit: 1000MS Memory Lim ...
- gIt 常用 操作
git提交代码流程git status -- 查看当前仓库状态git add -- 添加到临时仓库git commit -m '注释' -- 添加到临时仓库git status -- 查看当前仓库 ...
- json-lib-2.5-jdk.jar 需要依赖的jar包
commons-lang3-3.1.jar commons-lang-2.5.jar ezmorph-1.0.6.jar commons-collections-3.2.1.jar commons-b ...
- 好用的 curl 抓取 页面的封装函数
由于经常使用php curl 抓取页面的内容,在此mark 平时自己封装的 curl函数,(其实 现在也开始用 Python 来爬了~ ^-^) /** * 封装curl方法 * @author Fr ...
- swap 内存不足
参考:https://stackoverflow.com/questions/5682854/why-is-the-linker-terminating-on-me-when-i-build-clan ...
- wap网站即手机端网页SEO优化注意事项及方法
定位和页面设计: 无论是PC端还是移动端,网站 都要考虑清楚消费群体的定位问题.虽然智能手机用户数量非常普及,但是要明白中国的大部分手机用户使用的还是2G网络,一直高 喊的3G.4G手机用户只有大约1 ...
- let和var以及const有什么区别
在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. for(var i=0;i&l ...
- eclipse新建Maven项目
1.在eclipse中安装maven插件 2.点击File->new->maven project,出现弹窗后点击next. 接着在弹窗Select an Archetype中,filte ...