150 lines
5.0 KiB
C#
150 lines
5.0 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace Game.JSON
|
|||
|
{
|
|||
|
public static class AnalyzeJSON
|
|||
|
{
|
|||
|
public static void Analyze(string data)
|
|||
|
{
|
|||
|
// Dictionary<string, string> dictionary = new Dictionary<string, string>();
|
|||
|
//
|
|||
|
// var deserializeObject = JsonConvert.DeserializeAnonymousType(data, new Dictionary<string, object>());
|
|||
|
//
|
|||
|
// Deep(deserializeObject, ref dictionary);
|
|||
|
|
|||
|
// 解析 JSON 到动态类型
|
|||
|
var result = JsonConvert.DeserializeObject<Dictionary<string, JToken>>(data);
|
|||
|
|
|||
|
// 创建最终存储用的字典
|
|||
|
var finalDictionary = new Dictionary<string, string>();
|
|||
|
|
|||
|
// foreach (var item in result)
|
|||
|
// {
|
|||
|
// if (item.Value is JArray)
|
|||
|
// {
|
|||
|
// // 序列化嵌套数组或对象为 JSON 字符串
|
|||
|
// finalDictionary[item.Key] = JsonConvert.SerializeObject(item.Value, Formatting.None);
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// // 直接将值转换为字符串
|
|||
|
// finalDictionary[item.Key] = item.Value.ToString();
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
Deep(result, ref finalDictionary);
|
|||
|
|
|||
|
// 打印最终字典中的内容
|
|||
|
foreach (var pair in finalDictionary)
|
|||
|
{
|
|||
|
Debug.Log($"{pair.Key}: {pair.Value}");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static void Deep(Dictionary<string, JToken> dic1, ref Dictionary<string, string> dic2)
|
|||
|
{
|
|||
|
foreach (var item in dic1)
|
|||
|
{
|
|||
|
if (item.Value is JArray)
|
|||
|
{
|
|||
|
// 序列化嵌套数组或对象为 JSON 字符串
|
|||
|
var str = JsonConvert.SerializeObject(item.Value, Formatting.None);
|
|||
|
var deserializeObject = JsonConvert.DeserializeObject<Dictionary<string, JToken>>(str);
|
|||
|
Deep(deserializeObject, ref dic2);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 直接将值转换为字符串
|
|||
|
dic2[item.Key] = item.Value.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// static void Deep(Dictionary<string, object> dic1, ref Dictionary<string, string> dic2)
|
|||
|
// {
|
|||
|
// foreach (var valuePair in dic1)
|
|||
|
// {
|
|||
|
// var deserializeObject = JsonConvert.DeserializeAnonymousType((string)valuePair.Value, new Dictionary<string, object>());
|
|||
|
// if (deserializeObject != null)
|
|||
|
// {
|
|||
|
// Deep(deserializeObject, ref dic2);
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// dic2.TryAdd(valuePair.Key, valuePair.Value.ToString());
|
|||
|
// Debug.Log($"Key: {valuePair.Key}, Value: {valuePair.Value}");
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
public static void Main(string data)
|
|||
|
{
|
|||
|
var detailsDictionary = new Dictionary<string, object>();
|
|||
|
|
|||
|
JObject jsonObject = JObject.Parse(data);
|
|||
|
foreach (var property in jsonObject.Properties())
|
|||
|
{
|
|||
|
if (property.Value is JArray)
|
|||
|
{
|
|||
|
detailsDictionary[property.Name] = ProcessArray((JArray)property.Value);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
detailsDictionary[property.Name] = property.Value.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Use the detailsDictionary as needed
|
|||
|
foreach (var keyValuePair in detailsDictionary)
|
|||
|
{
|
|||
|
Debug.Log(keyValuePair.Key + " + " + keyValuePair.Value);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static object ProcessArray(JArray array)
|
|||
|
{
|
|||
|
var list = new List<object>();
|
|||
|
foreach (var item in array)
|
|||
|
{
|
|||
|
if (item is JObject)
|
|||
|
{
|
|||
|
var nestedDictionary = new Dictionary<string, object>();
|
|||
|
foreach (var prop in ((JObject)item).Properties())
|
|||
|
{
|
|||
|
nestedDictionary[prop.Name] = ProcessToken(prop.Value);
|
|||
|
}
|
|||
|
|
|||
|
list.Add(nestedDictionary);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
list.Add(item.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
static object ProcessToken(JToken token)
|
|||
|
{
|
|||
|
switch (token.Type)
|
|||
|
{
|
|||
|
case JTokenType.Array:
|
|||
|
return ProcessArray((JArray)token);
|
|||
|
case JTokenType.Object:
|
|||
|
var nestedDictionary = new Dictionary<string, object>();
|
|||
|
foreach (var prop in ((JObject)token).Properties())
|
|||
|
{
|
|||
|
nestedDictionary[prop.Name] = ProcessToken(prop.Value);
|
|||
|
}
|
|||
|
|
|||
|
return nestedDictionary;
|
|||
|
default:
|
|||
|
return token.ToString();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|