Frame/Assets/Scripts/JSON/AnalyzeJSON.cs

231 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace Game.JSON
{
public static class AnalyzeJSON
{
/// <summary>
/// 把json数据解析到一个字典里面必须保证key不会重复
/// </summary>
/// <param name="data"></param>
public static void AnalyzeToDic(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();
}
}
}
#region 解析到字典里面
/// <summary>
/// 解析到一个字典的结构类型的里面去
/// </summary>
/// <param name="json"></param>
public static Dictionary<string, object> AnalyzeToDicDeep(string json)
{
// 假设JSON字符串是这样的一个结构
string jsonString = json;
JArray jsonArray = JArray.Parse(jsonString);
var resultDictionary = new Dictionary<string, object>();
ProcessJsonElement(jsonArray, resultDictionary, "Items");
// 输出字典以检查结果
PrintDictionary(resultDictionary, "");
return resultDictionary;
}
private static void ProcessJsonElement(JToken token, Dictionary<string, object> dict, string key)
{
if (token.Type == JTokenType.Object)
{
var subDict = new Dictionary<string, object>();
foreach (var prop in ((JObject)token).Properties())
{
ProcessJsonElement(prop.Value, subDict, prop.Name);
}
dict[key] = subDict;
}
else if (token.Type == JTokenType.Array)
{
var list = new List<Dictionary<string, object>>();
foreach (var item in token.Children())
{
var subDict = new Dictionary<string, object>();
ProcessJsonElement(item, subDict, "Item");
list.Add(subDict);
}
dict[key] = list;
}
else
{
dict[key] = token.ToString();
}
}
private static void PrintDictionary(Dictionary<string, object> dict, string indent)
{
foreach (var pair in dict)
{
Debug.Log($"{indent}{pair.Key}:");
if (pair.Value is Dictionary<string, object> subDict)
{
PrintDictionary(subDict, indent + " ");
}
else if (pair.Value is List<Dictionary<string, object>> list)
{
foreach (var item in list)
{
PrintDictionary(item, indent + " ");
}
}
else
{
Debug.Log($"{indent} {pair.Value}");
}
}
}
#endregion
// 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();
}
}
}
}