收集 JSON 物件的所有欄位
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
public class JsonFieldsCollector
{
private readonly Dictionary<string, JValue> fields;
public JsonFieldsCollector(JToken token)
{
fields = new Dictionary<string, JValue>();
CollectFields(token);
}
private void CollectFields(JToken jToken)
{
switch (jToken.Type)
{
case JTokenType.Object:
foreach (var child in jToken.Children<JProperty>())
CollectFields(child);
break;
case JTokenType.Array:
foreach (var child in jToken.Children())
CollectFields(child);
break;
case JTokenType.Property:
CollectFields(((JProperty) jToken).Value);
break;
default:
fields.Add(jToken.Path, (JValue)jToken);
break;
}
}
public IEnumerable<KeyValuePair<string, JValue>> GetAllFields() => fields;
}
用法:
var json = JToken.Parse(/* JSON string */);
var fieldsCollector = new JsonFieldsCollector(json);
var fields = fieldsCollector.GetAllFields();
foreach (var field in fields)
Console.WriteLine($"{field.Key}: '{field.Value}'");
演示
對於此 JSON 物件
{
"User": "John",
"Workdays": {
"Monday": true,
"Tuesday": true,
"Friday": false
},
"Age": 42
}
預期輸出將是:
User: 'John'
Workdays.Monday: 'True'
Workdays.Tuesday: 'True'
Workdays.Friday: 'False'
Age: '42'