using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace UnitTest
{
class Program
{
static void Main(string[] args)
{
var json = "[{\"这是info1.key1\":\"这是info1.value1\",\"这是info1.key2\":\"这是info1.value2\"},{\"这是info2.key1\":\"这是info2.value1\",\"这是info2.key2\":\"这是info2.value2\"}]";
var list = JsonConvert.DeserializeObject(json) as JArray;
var listdata = new List<JToken>();
foreach (var item in list)
{
switch (item.Type)
{
case JTokenType.Object:
ChildrenTokens(item, listdata);
break;
case JTokenType.Property:
listdata.Add(item);
break;
default:
throw new Exception($"暂未适配该类型");
}
}
//遍历JProperty
foreach (var item in listdata)
{
var jProperty = item.ToObject<JProperty>();
string name = jProperty.Name;
JToken value = jProperty.Value;
}
Console.Read();
}
private static void ChildrenTokens(JToken jObject, List<JToken> jTokens)
{
if (jTokens == null) throw new Exception("接收对象不能为空");
var childrens = jObject.Children();
foreach (var item in childrens)
{
switch (item.Type)
{
case JTokenType.Object:
ChildrenTokens(item, jTokens);
break;
case JTokenType.Property:
jTokens.Add(item);
break;
default:
throw new Exception($"暂未适配该类型");
}
}
}
}
}